Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 547  / 1 Year ago, thu, november 24, 2022, 7:40:05

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.


More From » bash

 Answers
0

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 ]


[#28067] Friday, November 25, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ainubt

Total Points: 496
Total Questions: 98
Total Answers: 126

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago
;