last updated: Wednesday, July 25, 2018

BASH COMMANDS

QUICK LINKS

top

Commands

dd; pvCopy image or iso file with progress monitoring:
sudo dd if=path/to/filename | pv | sudo dd of=/dev/device_name
md5sum; pvProgress Bar for md5sum:
pv some.iso |md5sum
rsync, gcpCopying files with progress bars:
rsyncv -av --progress source destination
[-a = archive mode; equals -rlptgoD (recursive; links; preserve permissions, modification times, group, owner)]
gcp [-r] source destination
pdftotextTo convert a pdf to text:
pdftotext -layout $file
findTo list all symbolic links in a directory:
find . -type l -ls
espeakFun with espeak:
cat ch05.txt | espeak -ven-us+f4 -s170
cpUse shell expansion to create backup files with fewer key strokes:
cp ~/filename{,~}
cp -v filename{,~0}.file_extension
brace expansionTo compare two files that follow same naming pattern:
diff -s part{1..2}.txt
awkTo print file/folder size and name:
ll |awk '{ print $5 "\t" $9 }'
ll |grep .*html.* |awk '{ print $5 "\t" $9 }'
for loop; shell expansion
for f in test__[5-9]_boo.txt ; do echo "boo" > $f ; done
for loop; seqUse seq and a for loop to create file in a numbered order:
for NUM in `seq 2 1 12` ; do cp -v template.html na_step_guide__step_0$NUM.html; done
for loop; shell expansionTo loop thru files in a folder:
for i in *.php; do sed -i 's/<table>/<table class=\"centre\">/' $i; done
for loop; shell expansionTo create the same directory in multiple sub directories:
for dir in */; do mkdir -- "$dir/tmp1"; done
for loop; shell expansionTo create the same directories in multiple sub directories:
for dir in */; do mkdir -- "$dir"/{tmp1,foo,bar,qux}; done
Loop through a file line-by-line:
while read -r line ; do basename "$line" ; done < songs.txt > song_filenames__acer.txt
awk; grepTo remove blank lines:
cat file.txt | awk NF
cat file.txt | awk 'NF != 0'
grep -v '^[[:blank:]]*$' file.txt
mp3infoTo get duration of audio file:
mp3info -p "%m:%s\n" "$mp3"
[apt-cache show] To get a more detailed description then apt-cache search
scd $APP |grep -i description-en |sed 's/\(Description\)-en/\1/' && scd $APP |grep "^ .*"
[parted] To determine the type of partition table:
sudo parted -l
[rename] To rename a file more easily and quickly then using mv:
rename s/PATTERN_TO_REPLACE/NEW_PATTERN/ filename(s)
[rename] To replace spaces in filenames with underscores:
rename 'y/ /_/' *
rename 'y/ /_/' filename
[rename] To keep part of a pattern:
rename -v 's/(ch[0-9][0-9]_)/ $1_/' *
lisDir ch01_ipsum.txt renamed as ch01__ipsum.txt
ch02_ipsum.txt renamed as ch02__ipsum.txt
ch03_ipsum.txt renamed as ch03__ipsum.txt
ch04_ipsum.txt renamed as ch04__ipsum.txt
To get size of folders using find and du [ex. 1]:
find . -name bak -type d | xargs du -ch
To get size of folders using find and du [ex. 2]:
find -name bak -type d -exec du -ch '{}' \; +
To get size of folders using find and du [ex. 3]:
du -m --max-depth 1 | sort -rn
[NULL] NULL:
NULL
To remove aria2c-downloaded torrent with fewer key strokes:
^aria2c^rm
[ln] To create a symbolic link:
ln -s Dropbox/02__bin ./bin
[sed] To duplicate the function of grep:
sed -n 's/pattern/&/p' <filename
sed -n 's/\(pattern\)/\1/p' <filename
[find] To recursively search files for a string from a specific folder on down:
find /path/to/folder/ -exec grep 'searchTerm' '{}' /dev/null \; -print
find /path/to/folder/ -exec grep -H 'searchTerm' '{}' \; -print
[find] To recursively search a specific filetype for a string from a specific folder on down:
find /path/to/folder/ -name '*fileType' -type f -exec grep 'searchTerm' '{}' /dev/null \; -print
find /path/to/folder/ -name '*fileType' -type f -exec grep -H 'searchTerm' '{}' \; -print
[find] To list mp3s most recently modified, including sub-folders:
find $1 -type f -name '*mp3' -exec stat --format '%Y :%y %n' {} \; | sort -n | cut -d: -f4- | cut -d " " -f3-
[find] To find all files owned by a user:
find . -user username -ls
find; grepTo search file names (including sub-folders) for a specific term and display path in matches:
find . -print |grep -i searchTerm
find; grepTo list all files (including path) with a given extension:
sudo find . -print | grep -i '.*[.]givenExtension'
[mplayer] To generate and listen to a play list in shuffle mode:
find -type f -iname \*.mp3 > playlist.txt
mplayer -shuffle -playlist playlist.txt
[Xubuntu] To launch GUI folder sharing utility:
shares-admin
To check for and delete duplicate files in temporary/new music folder:
[ -e "../../${mp3}" ] && rm -v "$mp3"
ffmpegConvert m4a to mp3:
ffmpeg -i "Some Song.m4a" -acodec mp3 -ac 2 -ab 192k "Some Song.mp3"
When installing packages freezes or generates error messages:
dpkg --configure -a
[Xfce] To disable panel tool tips:
xfce4-panel --plugin-event launcher:disable-tooltips:bool:true
[convert] .flac to .mp3 (flac and lame required)
for f in *.flac; do flac -cd "$f" | lame -b 320 - "${f%.*}".mp3; done
[smem] To view memory usage in pie graph:
smem --pie name -s rss
[mtr] To get network diagnostics information similar to that provided by ping and traceroute:
mtr 8.8.8.8
[Ubuntu-specific] To get install date (method 1/2):
ls -l /var/log/installer
[Ubuntu-specific] To get install date (method 2/2):
sudo dumpe2fs /dev/sda1 | grep 'Filesystem created:'
top