Fooling around on Linux Command Line

I have been a long time Linux user. I installed Slackware 2.3 with kernel version 1.2.8 which came with PC Quest magazine on a CD in 1996 on my then DOS 5.5 and Windows 3.1 PC. I became a Mandriva fan when I visited their booth at the Linux expo in 1999 held in New York and they gave me a CD of Mandriva 6.0. Today, I frequent between Mageia and Ubuntu. But there is no end to the things I keep learning on how to do things faster, better etc. in Linux and all from the command line.

I hope that this page will be a live document of some nice and some very useful Linux command line tricks. Here goes:

running the last command as Root sudo !!
find your external IP address curl
empty any file without removing it > filename.txt
execute a command without saving it in the history _space_ command
backup a file before editing cp filename{,.bak}
traceroute with ping mtr
clear terminal screen ctrl-L
list of commands you use most often history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
run your previous command but replace “abc” with “xyz” !!:gs/abc/xyz
combine two pdf files gs -q sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER pdfFile1.pdf pdfFile2.pdf -sOutputFile=newFile.pdf
extract pages from a pdf file gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=startPageNumber -dLastPage=endPageNumber -sOutputFile=extractedPagesFile.pdf originalFile.pdf
identify the process listening on a port netstat -tulpn; ls -l /proc/processId/exe
find the process PID that opened port ABCXYZ fuser ABCXYZ/tcp or fuser ABCXYZ/udp
current working directory of a pid ABC ls -l /proc/ABC/cwd
owner of a process ABC ps aux | grep ABC
extract a small part of a video file starting at time specified by -ss and for time specified by -t ffmpeg -ss 00:00:30 -t 00:00:05 -i orginalfile -vcodec copy -acodec copy newfile
truncate a file to size 0 truncate -s 0 {filename.txt} OR cat /dev/null > largefile.txt OR cp /dev/null largefile.txt
passwordless SSH logins from a@A to b@B On A as 'a'
  1. ssh-keygen -t rsa -b 2048 (Don't enter a passphrase)
  2. ssh b@B mkdir -p .ssh
  3. cat .ssh/ | ssh b@B 'cat >> .ssh/authorized_keys'
  4. Done
create a colourful bash prompt export PS1="\e[0;32m[\u@\h \W]\$ \e[m " will give you a nice green prompt. Substitute 32 with 31 for red, 34 for blue and 36 for cyan. Check this list for more fun prompts and colours.
Sending HTTP POST requests from command line curl -d "param1=value1&param2=value2&param3=value3"
Sorted list of space used in a folder du --block-size=GiB --max-depth=1 | sort -n
See free space on HDD df -h
Space used by files and folders in current directory du -sh *
Amount of free memoryin MB vmstat 1
Ping an Internet host and report packet loss statistics after 5 packets mtr -c 5 -r ip_address_value
List programs listening on a socket netstat -nlp
Display the current routing table netstat -rn
Display CPU and disk statistics iostat
Monitor your CPU/GPU temperatures and fan speeds in real time (needs lmsensors) watch sensors
To run many commands in sequence in one line cmd1; cmd2; cmd3; cmd4
Execute the second command only if the first succeeds cmd1 && cmd2
Execute the second command only if the first command fails cmd1 || cmd2
Execute a command on everything except pdf files cmd1 !(*.pdf)
Scan the ports 1-1023 of a host nc -zv host 1-1023
Reverse SSH tunneling from A to B using C On B: ssh -R 19999:localhost:22 user1@C
On A: ssh userX@C
After login (on C): ssh localhost -p 19999
Grep a UTF-16 file iconv -f utf-16 -t utf-8 file.txt | grep query
Search a tcpdump file for DNS URLs tshark -r Network\ Traffic.pcapng -T fields -e ip.src -e -Y "dns.flags.response eq 0 and contains"
Systemd: Show data from most recent boot journalctl -b
Systemd: Show verbose error logs about NetworkManager since yesterday journalctl -b -u NetworkManager --since='yesterday' -p err -o verbose
Systemd: Show all boots journalctl --list-boots
Systemd: Show all available fields within a boot's logs journalctl -F _BOOT_ID
Systemd: Show all logs related to a particular executable in the last two hours journalctl _COMM=programname --since='2 hours ago'
To execute a PHP function from command line php -r "require 'filename.php'; function_name('arg1', 'arg2' , 'arg3');"
To kill a frozen SSH session Hit the Enter key. Type ~ followed by a single period i.e. .
For other escape sequences Hit the Enter key, type ~ followed by ?

Last Update: