.tar is a sinple bundle of files, without compression
.tar.gz with compression
usage
1
2
3
4
5
6
7
8
9
10
11
# [c]reate an archive and write it to a [f]ile:tar cf path/to/target.tar path/to/file1 path/to/file2 ...
# [c]reate a g[z]ipped archive and write it to a [f]ile:tar czf path/to/target.tar.gz path/to/file1 path/to/file2 ...
# E[x]tract a (compressed) archive [f]ile into the current directory [v]erbosely:tar xvf path/to/source.tar[.gz|.bz2|.xz]# E[x]tract a (compressed) archive [f]ile into the target directory:tar xf path/to/source.tar[.gz|.bz2|.xz] --directory=path/to/directory
# Find all directories named srcfind . -name src -type d
# Find all python files that have a folder named test in their pathfind . -path '*/test/*.py' -type f
# Find all files modified in the last dayfind . -mtime -1
# Find all zip files with size in range 500k to 10Mfind . -size +500k -size -10M -name '*.tar.gz'# Find directories matching a given name, in case-insensitive mode:find root_path -type d -iname '*lib*
1
2
3
4
# Delete all files with .tmp extensionfind . -name '*.tmp' -exec rm {}\;# Find all PNG files and convert them to JPGfind . -name '*.png' -exec convert {}{}.jpg \;
# Find all python files where I used the requests libraryrg -t py 'import requests'# Find all files (including hidden files) without a shebang linerg -u --files-without-match "^#\!"# Find all matches of foo and print the following 5 linesrg foo -A 5# Print statistics of matches (# of matched lines and files )rg --stats PATTERN
$_ Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc followed by . or Alt+.
$@ 是 $1, $2… 的集合
sudo !! 展开就是上一条命令, 在执行必要用 sudo 的命令好用
$((expression)) stores the result, example: var=$((3+9))
foo=$(pwd) 中的 $() 是 pwd 命令结果
Practices
与上一条参数相同的话:
1
2
mkdir testcd$_
遍历程序参数:
1
2
for file in "$@":
...
对必须接受文件的为参数的程序很有用:
ls 的结果会存在临时文件里交给 cat
1
cat <(ls)
遍历当前目录很简单:
1
for file in $(ls)
快速比较目录:
1
diff <(ls foo) <(ls bar)
shebang
1
#!/bin/bash
特殊变量的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
echo"Starting program at $(date)"# Date will be substitutedecho"Running program $0 with $# arguments with pid $$"for file in "$@";do grep foobar "$file" > /dev/null 2> /dev/null
# When pattern is not found, grep has exit status 1# We redirect STDOUT and STDERR (2>) to a null register since we do not care about themifspan> ;thenecho"File $file does not have any foobar, adding one"echo"# foobar" >> "$file"fidone
set loglevel to panic, take a single frame from webcam device as a image, print to STDOUT rather than give a file(-), convert read from STDIN(-), convert to grey colorspace and write to STDOUT(-), compress and send to server tsp, make copies both file and STDOUT, bring it back to local stream and feh displays