This page provides a few tips concerning shell commands, and in particular the Bash shell.
History
To clear history:rm ~/.bash_history ./history -c
Loops
Add a .class extension to all files in the current directory:for file in `ls -1 .`; do mv $file $file.class; doneRenaming a range of files:
for i in {2..7}; do mv Screenshot000$i.jpg blah-$i.jpg; doneSmarter renaming:
for i in `ls -1`; do mv "$i" "${i/mar2014/2014-03}"; doneRename a list of files:
for i in 1070 1227 1157; do echo "./pictures/IMG_$i.jpg"; doneCopying a list of files via scp:
cat ./filenames| while read p; do scp user@host:./path/$p . ; done
Text formatting
Format a text with max 80 columns width:fmt -w 80 input.txt > output.txtTo columnate lists:
column -s ';' -t
Tar
To untar in a given directory:tar my.tar -C dir
Zip/Unzip
When unzip fails, try 7za:7za e thezip.7zTo unzip without the arborescence:
unzip -j thezip
Sed
Removes last character of each line:sed 's/\(.*\)./\1/'Remove the firsst 5 characters of each line:
sed -e 's/^.....\(.*\)$/\1/g'Deletes all lines starting with #
sed '/^#/ d'Delete first line:
sed '1d'Like dos2unix:
sed -i 's/\r//' fileTo display line number n:
sed 'NUMq;d' fileCut
echo blah-date.eps.png |cut -d\. -f1 blah-dateExample: renaming all .eps.png to .eps:for i in `ls -1 *.eps.png`; do mv $i `echo $i | cut -d\. -f1`.png; doneecho
Echo normally outputs a \n. To remove the trailing newline use -n:echo -n "toto" | sha1sumfind
Print all directories:find . -type d -exec echo {}\; -printFind a keyword in a given directory:find DIR | xargs grep KEYWORDCount lines in a directory:wc -l `find . -type f`Remove empty files:find -maxdepth 1 -type f -empty -print0|xargs -0 rm -f => remove empty filesFind duplicates:find . -type f -exec sha1sum "{}" \; | sort | uniq -d -w40Find file modified less than 10 minutes ago:find . -mmin -10Find file older than 365 days:find . -mtime +365 ...Archive files less old than one week:find / -mtime 7 | xargs tar -zcf weekly.tgzgrep
grep -o -E "\".*\"" --color=always file.txtThis command will:
- -o: print only matching lines of file.txt
- -E: specifies a regular expression. In this case, we are going to match any text between quotes. In the regular expression, the quotes must be escaped.
- --color: put colour to the matching regular expression. This is much easier to read
curl
Use -F to POST information.curl -F `username=blah` -A 'USER AGENT` http://MY-URL
awk
To get specific columns of an output line:ls -l | awk '{print $6" "$7" " $8}'To remove duplicate lines in a file:
awk '!_[$0]++' fileFind non empty files in a tar:
tar tfvj blah.tar.bz2 | awk '$3 > 0 {print $3 " " $6}'To perform a sum:
... | awk '{ sum += $1 } END { print sum }'
Sort
Numeric sorting:sort -nk 2
Comm
Compares to files line by line:comm -23 a.sorted b.sorted > diffex: where -2 suppresses column 2, -3 suppresses column 3.
BC
Convert an hexadecimal number:echo "ibase=16; 5F" | bc 95
ps
ps -o pid,ppid,comm,start,class,fuser -p 10617,10615
Converting...
Converting a text to pdf:asciidoctor -b pdf mytext.txt