paskvilwiki

up: servers & admin

Bash

last updated 2013-10

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

commands, tricks and lsof

commands and shortcuts

$ sudo !!                         - run last command as root
$ !!:gs/foo/bar                   - run last command, replacing 'foo' with 'bar'
$ ^foo^bar                        - run last command, replacing 'foo' with 'bar'
$ cp filename{,.bak}              - create a .bak copy of file
<Ctrl-x> <Ctrl-e>                 - open $EDITOR to edit current command line; upon exit, content will be run
$ <space>command                  - run the command without saving it in history
$ > file.txt                      - empty a file
<Alt+.>                           - put last argument on cursor place; repeat to cycle through
$ (cd /tmp && ls)                 - jump to /tmp, execute ls, and jump back here
$ pushd /tmp .... $ popd          - goes to /tmp, remembering current location; at later time, popd will bring you back
$ \command                        - run command without using aliases
$ rm !(pattern|pattern)           - delete files that do not match given patterns
$ find . -mtime +5 -exec rm {} \; - delete all files in this folder that are older than 5 days
<Ctrl-u> ... <Ctrl-y>             - Ctrl+u will cut current command, then you can do whatever you need, and Ctrl+y will paste it back
$ date -d @1234567890             - convert unix timestamp to human-readable
$ jul () { date -d "$1-01-01 +$2 days -1 day" "+%Y%m%d"; }   - `jul $year $daynum` prints $daynum's day of $year, with Jan 1st being day 1

info

$ mount | column -t               - pretty-print mount table
$ cat /etc/issue                  - display distro name

small tricks

$ echo $(printf '%q' "A\"B")      - prints escaped string (in this case, > A\"B <)
$ mount -t tmpfs tmpfs /mnt -o size=1024m      - mount temporary RAM partition (fast access, non-persistent!)
$ disown -a && exit               - exit the shell while keeping all running processes alive
$ ssh -t remote_host screen -r    - directly connect to a remote screen process
$ for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
                                  - backup all databases into individual files
$ fuser -k filename               - kill process that is locking given file
$ readom dev=/dev/scd0 f=/path/to/image.iso    - create CD/DVD ISO image
$ diff -rq folder-1 folder-2      - compare 2 folders (files missing or differing)
$ mkdir -p a/deep/dir/tree        - create nested directories fast
$ mount file.iso /mnt/iso -o loop - mount ISO file as drive /mnt/iso (dir has to preexist)
$ sudo touch /forcefsck           - force a file system check on the next reboot
$ ls | while read f; do process_file "$f"; done    - iterate through files even if they contain space in name
$ while read f; do process_file "$f"; done < <(ls) - iterates through files, but does not create sub-process (outside scope is visible)

lsof

$ lsof -nPi                       - list network connections
$ netstat -tlnp                   - print all listening ports with PID's (run as root to see all process names)
$ lsof -c gnome-terminal          - list files opened by given command
$ lsof +D [folder]                - discover all open files in the [folder]
$ lsof -p [pid]                   - list files opened by given PID
$ lsof -i :8080                   - list programs that have port 8080 open ("who to kill" if you get "port already in use")
$ if [ `lsof -- file | wc -l` -ne 0 ] ... - tests if file is open by some process

cURL in the terminal

use -s to make curl silent

POST to a server

curl --data "key1=value1&key2=value2" http://www.example.com/post.php

upload to an FTP server

with credentials, 60 seconds connection timeout and 120 seconds maximum operation time:

curl --connect-timeout 60 -m 120 -T file.ext "ftp://12.34.56.78/folder" --user user:pass

download from an FTP server

with credentials:

curl --user user:pass "ftp://12.34.56.78/folder/file.ext" -o ~/Downloads/file.ext