How do I check if a file called /tmp/foo.txt exists or not using a shell script under Linux operating systems?
You can use test command or [ to check file types and compare values. The -f option check if FILE exists and is a regular file. If it exists it returns true value. You can test it as follows for a shell prompt itself:
$ [ -f /tmp/foo.txt ] && echo "File Exists" || echo "File not found"
Bash Script To Check If a File Exists
#!/bin/bash file=$1 [ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 999; } if [ -f $file ]; then echo "File $file exists." else echo "File $file does NOT exists." fi
Run it as follows:
$ ./script.sh /etc/resolv.conf
File /etc/resolv.conf exists.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop






![Bash Shell Scripting Disable Control-C [ CTRL+C ] Keys](http://s13.cyberciti.org/images/shared/rp/3/14.jpg)






{ 0 comments… add one now }