Handy Linux Commands

Posted by Paul on June 10, 2015

Just a little page for some regularly used useful Linux commands:

Sort directories by size:
du --max-depth=1 . | sort -n -r

Show files modified in the last 1 day:
find . -mtime -1

Show the most recently modified files recursively:
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head

Zip
zip -r yourfiles.zip yourfiles

Unzip
unzip yourfiles.zip
(Puts files loose in the current dir unless you add "-d directory" in which case it creates directory).

Automatically Unzip and Create Folder for Various Zip Types
dtrx yourarchive.zip|.tar.gz|.7zip etc.

Tar (-c = create, -f = file, [-v = verbose, -z gzip])
tar -cf yourfile.tar directory/

Untar (-x = extract, -f = file, [-v = verbose, -z gzip])
tar -xf yourfile.tar

Mount an FTP location as a local filesystem once (curlftpfs is installed):
curlftpfs ftp-user:[email protected] /mnt/my_ftp_folder/

Search for a string recursively an case insensitively accross multiple files:
grep -ri "string" .

List the top 30 biggest folders in the current dir:
du -h --max-depth=1 . | sort -h -r | head -n 30

List the top 30 biggest files recursively from the current dir:
find . -type f -printf '%s %p\n'| sort -rh | head -30 | numfmt --field=1 --to=iec

List the top 30 biggest zip files recursively from the current dir:
find . -type f -iname "*.zip" -printf '%s %p\n'| sort -nr | head -30

Send an email from the command line:
echo “This will go into the body of the mail.” | mail -s “Hello world” [email protected] -a "From: [email protected]

Monitor system resource usage (after installing sysstat):
sar {interval in seconds} {number of repititions} 

Quickly Login to a VPS
sshpass -p 'rootpassword' ssh [email protected]

Hit All Pages of a Website (4 levels deep) - useful if you've cleared the cache and it doesn't automatically regenerate:
wget --spider -r -l4 website-to-spider.com

Speed up your mouse beyond allowed settings (first number is speed, second number is the threshold - sort of precision at slow speeds):
xset m 8 4
xset m 2 0
xset m 3 10

Change all folders to 755
find . -type d -print0 | xargs -0 chmod 0755

Change all files to 644
find . -type f -print0 | xargs -0 chmod 0644

List Duplicate Files Recursively
fdupes -r .

Delete Duplicate Files Recursively
fdupes -rd .

Creating a Bootable USB Drive
Find the device:
lsblk
(Shows something like "sdb")
Put the iso on to the USB device from above:
dd if=youriso.iso of=/dev/sdb bs=4M status=progress oflag=sync

Clear Terminal Including Scroll-Back
You can press Ctrl + L or type clear to clear the screen but if you scroll up the old content is there and may confuse you if you're looking at the output of a long command. This will only be in an SSH session - clear works fine in a normal terminal but in SSH you can accidentally scroll back past where you typed "clear". The below will make it so that when you scroll up you'll only be able to go as far as where you ran this command:
clear && printf '\e[3J'

Can you think of any you'd add? Feel free to comment below!

blog comments powered by Disqus