0

I'm confused between two tages that can be used in if statement in bash scripting.

One is -f and other is -a.

Their description states as follows:

[ -f FILE ] True if FILE exists and is a regular file.
[ -a FILE ] True if FILE exists.

Now, while using the if condition in my shell script how can I know which tag should I use. Like I have a script which takes regular backups using tar and creates a file file-name.tar.gz so how can I know which is a regular file. Thanks.

2 Answers 2

2

Regular files are all the text or data file, it can be a binary file as well. For regular files the output of ls -l would be like,

-rw-rw-r-- 1 username group 74 Nov 27 16:03 mytext.txt

In general the first place for a regular file a - should appear. In such cases if [ -f FILE ] would be appropriate.

Non-Regular files are devices, pipes, sockets, tty etc. all other that fall under "Everything is a file" philosophy as pointed out by drc. You can go through Unix_file_types for more information.

You can verify the ls -l output of some like,

crw-rw---- 1 root tty 4, 1 Dec  9 13:41 /dev/tty1
brw-rw---- 1 root disk 1, 11 Dec  9 13:41 /dev/ram11

For all the non-regular files you should use if [ -a FILE ]

1
  • Though symbolic links are non-regular, if [ -f FILE ] works for them. see this
    – sourav c.
    Dec 9, 2013 at 9:37
1

For tar files – and basically any other file you create – -f is the correct choice.

-a is for special files like device nodes, fifos and such, which according to the Unix philosophy of Everything is a file are presented as files in your filesystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.