Skip to content

Linux Tricks and Useful Commands

pwd -P # Print path to the dir you are in, converting any symlinks in the path into their real/(P)hysical directory name.
grep -ril inactive /etc # Show matching files(-l) with the string “inactive” regardless of case(-i) in all subdirs of /etc (-r)

for i in {0..365};do date -d “Jan 1 2012 + $i day” +”*%B %_d – “; done # Generate days of the year list with some formatting. Req. GNU date

sox song.wav -t wav – pitch 1200 | play - # Play a song an octave higher without saving an intermediary file. 1200 cents == 1 octave

ascii || man ascii # Quick access to the ASCII character table either through the ascii program or the man page if you don’t have the prog.

ionice -c 3 cp vm1.img vm1-clone.img # Copy a file using “ionice -c 3″ to give it idle IO priority to reduce load on the system.

mv somedata{,-20120720} # This is the same as mv somedata somedata-20120720 but is shorter. You don’t have to type somedata twice.

foremost # Foremost is a file recovery program that can extract files from disk images and more. # GNU ddrescue is a good tool for copying/recovering disk images.

dd if=/dev/sdc of=sdc.img bs=100M conv=sync,noerror # Image a disk, sync,noerror will continue on read errors and pad missing parts.
kill -USR1 $( pidof dd ) # Tell each dd process to print its current status (blocks written, etc). dd interprets the USR1 signal this way.

ls -d */ # View only the directories in the current directory. The / at the end of the wildcard makes this work.

convert -rotate -90 sideways.jpg rightsideup.jpg # Rotate an image 90 degrees counterclockwise. The convert command is from ImageMagick

gzip -l largefile.gz # A fast way to get the size of the uncompressed gzip file. However the uncompressed value can’t be more than 4GB.

montage -geometry 800×600+4+4 pic1.jpg pic2.jpg pic3.jpg pic4.jpg -tile x2 combined.jpg # Make 2×2 800×600 montage with 4 px border.

echo “1-800-SHOWNUM” | tr A-Z a-z | tr ‘a-z’ ’22233344455566677778889999′ # Looks like a command that we could make shorter

[Ctrl-w] # Remove the previous word on the command line (before the cursor). This is highly useful when reusing old commands.

[Ctrl-l] # This is usually equivilent to running ‘clear’. Its usually quicker and doesn’t leave the command in your command line history.

[Ctrl-e] # Move your cursor to the end of the line. Faster than holding down right arrow

[Ctrl-a] # Move your cursor to the beginning of the line. Faster than holding down left arrow. In screen, you need to press

[Ctrl-r]string # Reverse search through your command history for ‘string’. Press Ctrl-r again to continue searching backwards. ESC when done

cd # Takes you back to your home directory.

cd - # Takes you back to the previous directory you were in. Good to know if you don’t already.

for d in /dev/sd? ; do sudo smartctl -H $d ; done # How are your drives doing? may need apt-get install smartmontools Thx

find dir1 dir2 dir3 | wc -l # Count the number of files in multiple directories.

echo “kernel.panic = 20″ | sudo tee -a /etc/sysctl.conf # so your headless Linux system reboots after a kernel panic (20 sec) Thx

getent services 993 # Look up in your services table to see what port 993 is. Can also look up by service name to get port.

ls -1 | tr A-Z a-z # List files and print them all in lowercase so you can copy and paste and compare with another list that got fatty.

getent passwd|while IFS=: read -r user n uid n n home n;do if [[ $uid -ge 500 ]];then printf “$user “;du -sh $home;fi;done # space per user

sleep 8h 30m 20 # In the GNU version of sleep you provide mixed time intervals to stack them together instead of just seconds.

sed -n ’1,10p’ # Probably your best option is to put something like this into a script called or whatever

ls -ltrah | sponge | pee head tail # This also works. sponge also comes with moreutils and kinda fits with the naming there.

ls -ltrah | pee “sed -n ’1,10p’” tail # Sorry, the last command suffers from head closing the input prematurely. So try this instead. # iodine is a client and server that allow you to tunnel IP traffic over DNS, bypassing many firewalls, etc.

alias dnsip=’dig +short’ # even gets your public ip behind pay hotspots.
df . # This is an easy way to find out what partition the current directory is on, regardless of symlinks.

curl “” |python -mjson.tool |grep ‘”text”:’ # Reformat JSON data and get latest updates
identify -format “%f F:%[EXIF:Flash]\n” *.jpg | egrep ” F:(0|16|24|32)$” # Show photo filenames where no flash was used.

identify -format “%f F:%[EXIF:Flash]\n” *.jpg | egrep ” F:(0|16|24|32)$” # Show photo filenames where no flash was used.
/usr/lib/xscreensaver/skyrocket # More fireworks! GUI output, but many CLI options. You need the rss-glx screensavers package.

date -d “$(grep finish /proc/mdstat |sed -e ‘s/^.*finish=//;s/ .*$//;s/\..*$//’)min” # Figure out when your md-raid will finish.

todo(){ cd ~/.todo||return 1&& l=$(ls -1t|head -n1)&&t=$(date +%Y%m%d);[[ "$1" == "last" ]]&&cp $l $t; ${EDITOR:-vi} $t;cd -;} # Todo list.

ls -l |sed ‘s/^/ /’ # Ever have one of those situations in console mode where you can’t read the first characters? This intents the output.

fsck -y /dev/sda1 || echo “You’re fscked!” | mail -s alert # Use || when you want to run a command only if the first one failed

service network start && service httpd start # Use && when you want to run a command after another only if the first one is successful

sleep 30m ; killall tcpdump # Use a ; in a statement if you want to run a command after another, but don’t care about the exit status of 1st
ssh-copy-id ‘user@remotehost’ # Automatically installs your public key to the remote host (this actually is included in the openssh package)

xargs -n1 -0 < /proc/$(pidof firefox| cut -d’ ‘ -f1)/environ # Print the environment of a running process (ie. firefox) awk ‘{if (a[$1]) { print; } else { a[$1]=1 }}’ md5sums.txt # And here is a way to print only duplicates using just awk. sort md5sums.txt | uniq -d -w 32 # Print only duplicate md5sums by looking only at first 32 chars for uniqueness. head -5 file1 |cat – file2 >combofile # One way of putting the first 5 lines of file1 before the contents of file2 and writing to combofile

last |awk ‘BEGIN {f=0;u=”YOURUSER”;} {if ($1==u && f==0){f=1; print $0;}else if(f==1 && $1!=u) {print $0;exit;}}’ # Display prev. user b4 u

grep -B2 Subj: maildrop.log | grep -v ^From: | grep -v ^– | xargs -d$’\n’ -n 2 # Join the Date and Subj line together in procmail log.

find . -type f | egrep -o “[^\.]+$” | tr A-Z a-z | sort | uniq -c # Show a count of all the file extensions in use below current directory.

awk ‘{a[$1] += $10} END {for (h in a) print h ” ” a[h]}’ access_log | sort -k 2 -nr | head -10 # Display top bandwidth hogs on website.

curl -s ‘…’ |grep -q ‘#500′ && echo “500 ready”|mail -s 500 you # site watcher
- ssh-keygen -R servername # Remove the host key for ‘servername’ from your known_hosts file. This is now the proper way to do it.

Next I’ll be showing 4+ different ways of doing the same thing, printing out the partition table entries for use in snmpd.conf.

df -TP | grep -E ” ext[34] ” | awk {‘print $NF’} | sed ‘s/.*/disk & 5%/’ # Print out the partition table and format it for snmpd.conf

df -TP | awk ‘$2=/ext[34]/ {print “disk ” $NF ” 5%”}’ # snmpd disk entries, same thing but let awk do most of the processing work.

df -P -t ext3 -t ext4 | grep / | awk ‘{print “disk ” $NF ” 5%”}’ # snmpd disk entries, Let df select the filesystems directly.

ethtool -p eth0 # Blink eth0′s LED so you can find it in the rat’s next of server cables. Ctrl-C to stop. Thanks

ls / fake 2>&1 > /dev/null | grep “cannot” # Redirect stdout to /dev/null, and make stderr go through the pipe.

df -TP | grep -E ” ext[34] ” | rev | cut -d’ ‘ -f1 | rev | while read -r fs ; do echo “disk $fs 5%” ; done # Disk entries. The Crazy Way!

ps aux | awk ‘/firefox/ {sum += $6} END { printf “%dMB\n”, sum/1024 }’ # Show the total memory used by Firefox processes (Probably a lot)

Tip ~ To test Internet connection speed from console over SSH command line:
## Download speed
lftp -e ‘pget; exit; ‘

## Upload speed
lftp -u userName -e ‘put largecd1.avi; bye’

## Get network throughput rate between two Linux or Unix servers:
iperf -s -B serverIP
iperf -c serverIP -d -t 60 -i 10

See for more info –

* More tips and howtos from our /faq/ section *
Debian / Ubuntu: Console Based Network Load Monitor or see a quick video demo –

Debian / Ubuntu Python: Set Planet Venus To Combine Two Or More RSS Feeds

OpenBSD: Boot / Install Using PXE (Preboot Execution Environment)

HowTo: Bash Extract Filename And Extension In Unix / Linux

HowTo: Bash Shell Split String Into Array

Sed: Find and Replace The Whole Line [ Regex ]

Bash Script: Find Out In What Directory Script Is Stored Under Unix Or Linux

find . -newerBt “24 hours ago” # Find files created in the last 24 hours. Note: This won’t work on all filesystems.

Posted in Linux.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.

I'm happy to use Increase Sociability.