Short Notes on Bash

From PaskvilWiki
Jump to: navigation, search

Extensions and File Name

$ fullname='/tmp/file.tar.gz'
$ filename=$(basename $fullname)
$ echo ${filename##*.}
>> gz
$ echo ${filename#*.}
>> tar.gz
$ echo ${filename%.*}
>> file.tar
$ echo ${filename%%.*}
>> file

For more options and explanations, see Bash - Shell Parameters Expansion.

File Test Operators

Operator Function
-e file exists
-f exists and is regular file (not directory or device file)
-s exists and is non-zero size
-d exists and is directory
-b exists and is block device
-c exists and is character device
-p exists and is pipe
-h exists and is symbolic link
-S exists and is socket
-t is associated with terminal device; e.g. [ -t 0 ] and [ -t 1 ] test whether stdin and stdout is terminal
-r user has read permission
-w user has write permission
-x user has execute permission
-O user is owner
-G user's group is the same as the file's group
-N file modified since it was last read
f1 -nt f2 f1 is newer than f2
f1 -ot f2 f1 is older than f2