Home
Up

History
Loops
Text formatting
Tar
Zip/Unzip
Cut
Sed
Echo
Find
Grep
Curl
Awk
Sort
Comm
BC
Converting...
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; done 
Renaming a range of files:
for i in {2..7}; do mv Screenshot000$i.jpg blah-$i.jpg; done
Smarter renaming:
for i in `ls -1`; do mv "$i" "${i/mar2014/2014-03}"; done
Rename a list of files:
for i in 1070 1227 1157; do echo "./pictures/IMG_$i.jpg"; done
Copying 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.txt
To 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.7z
To 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//' file
To display line number n:
sed 'NUMq;d' file

Cut

echo blah-date.eps.png |cut -d\. -f1
blah-date
Example: renaming all .eps.png to .eps:
for i in `ls -1 *.eps.png`; do mv $i `echo $i | cut -d\. -f1`.png; done

echo

Echo normally outputs a \n. To remove the trailing newline use -n:
echo -n "toto" | sha1sum

find

Print all directories:
find . -type d -exec echo {}\; -print
Find a keyword in a given directory:
find DIR | xargs grep KEYWORD
Count 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 files
Find duplicates:
find . -type f -exec sha1sum "{}" \; | sort | uniq -d -w40
Find file modified less than 10 minutes ago:
find . -mmin -10
Find file older than 365 days:
find . -mtime +365 ...
Archive files less old than one week:
find / -mtime 7 | xargs tar -zcf weekly.tgz

grep

grep -o -E "\".*\"" --color=always file.txt
This 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]++' file
Find 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 > diff
ex: 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