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:ftp-pass@my-ftp-location.com /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” to@to.com -a "From: from@from.com" 

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

Quickly Login to a VPS
sshpass -p 'rootpassword' ssh root@ipordomain

Hit All Pages of a Website (4 levels deep) - useful if you've cleared the cache and it doesn't automatically regenerate:
wget --reject '*.js,*.css,*.jpg,*.jpeg,*.png' -nd -nv --spider -r -l4 https://www.antropy.co.uk/

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'

Find Large Log Files Recursively
find . -name '*.log' -o -name '*.txt' -size +100k -exec ls -lh {} \;

Rename Multiple Files (change abc to def in all pdf files (s=substitute))
rename 's/abc/def/' *.pdf

Transfer Files Between Servers with rsync
First cd to the directory where you want the files to end up. You could try using ssh to the remote server as it sometimes asks you to confirm you accept the fingerprint. You can also make sure you have the correct full path from the remote host.
-a = archive - which in this case means preserve folder structure and permissions etc.
-v = verbose - detailed output
-z = compress - speeds up the transfer
-e = allows use of a custom program other than ssh, or in this case allows you to pass options to ssh
Make sure the path/to/folder/on/remote/server/ has a trailing slash to copy the files inside the folder (including hidden files) rather than the folder itself.
rsync -avz -e 'ssh -p {custom-port}' {user}@{remote-host}:path/to/folder/on/remote/server/ .

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

blog comments powered by Disqus