Quick Reference Guide to Common Linux Commands

Quick Reference Guide to Common Linux Commands

This article lists various practical Linux commands from aspects such as backup and compression, file system search, networking, arithmetic operations and conversions, text processing, date and time, multimedia, disk usage and management, and hardware information, serving as a reference guide.

1. Backup and Compression

Command Description
<span><span>tar -c scripts/ | bzip2 -9 > scripts.tar.bz2</span></span> Compress the <span><span>scripts</span></span> directory using <span><span>tar</span></span> and <span><span>bzip2</span></span> with maximum compression ratio
<span><span>dd if=/dev/sda1 | gzip -c9 > /media/usb/sda1.dd.gz</span></span> Backup and compress the partition <span><span>/dev/sda1</span></span> to the local file <span><span>sda1.dd.gz</span></span>
<span><span>cat /media/usb/sda1.dd.gz | gzip -d | dd of=/dev/sdX1</span></span> Restore the compressed backup of the <span><span>/dev/sda1</span></span> partition from the file to the <span><span>/dev/sdX1</span></span> partition
<span><span>dd bs=1M if=/dev/sda | gzip -c9 | ssh [email protected] 'dd of=sda.dd.gz'</span></span> Create a compressed backup of the hard drive <span><span>/dev/sda</span></span> and create a remote copy using <span><span>ssh</span></span>
<span><span>find /etc/ -name '*.conf' | tar -c --files-from=- | bzip2 -9 > system_confs.tar.bz2</span></span> Find all configuration files in the <span><span>/etc/</span></span> directory (<span><span>*.conf</span></span>) and compress them into the file <span><span>system_confs.tar.bz2</span></span>
<span><span>dd if=/dev/sdb of=my.mbr bs=466 count=1</span></span> Backup the Master Boot Record (MBR) of the hard drive <span><span>/dev/sdb</span></span> to the file <span><span>my.mbr</span></span>
<span><span>dd if=my.mbr of=/dev/sdX bs=466 count=1</span></span> Restore the Master Boot Record (MBR) from the file <span><span>my.mbr</span></span> to the hard drive <span><span>/dev/sdX</span></span>
<span><span>wget --mirror https://example.com</span></span> Create a complete mirror of a remote website using <span><span>wget</span></span>
<span><span>tar cvjf etc_$(date +%Y%m%d).tar.bz2 /etc/</span></span> Create an archive of the <span><span>/etc/</span></span> directory using <span><span>tar</span></span> and compress it with <span><span>bzip2</span></span>. The compressed file name will include the current date
<span><span>tar xvjf etc.tar.bz2</span></span> Extract the <span><span>bzip2</span></span> archive file <span><span>etc.tar.bz2</span></span>
<span><span>find /var/www/ -name '*.gif' | xargs cp -va --target-directory=/tmp/gifs</span></span> Find all GIF files in <span><span>/var/www/</span></span> and copy them to the <span><span>/tmp/gifs</span></span> directory
<span><span>ssh [email protected] '( mysqldump --password='pass' data > data.sql )'</span></span> Create a MySQL backup of the <span><span>data</span></span> database to the remote file <span><span>data.sql</span></span>
<span><span>split -b 1000m linux-commands.iso</span></span> Split the file <span><span>linux-commands.iso</span></span> into 1GB files (generating xaa, xab, xac… etc.). Useful when dealing with FAT32 file systems.
<span><span>cat xa* > linux-commands.iso</span></span> Merge the split files back into <span><span>linux-commands.iso</span></span> (see the previous command)

2. File System Search

Command Description
<span><span>find /opt -name 'pass*' -or -size +1000k</span></span> Find files in the <span><span>/opt</span></span> directory whose names start with <span><span>pass</span></span> or have a size greater than or equal to 1000k. Other boolean operators such as -and and <span><span>-not</span></span> can also be used.
<span><span>locate -r '[^/]*\.conf'</span></span> Search the index and locate all files with the extension <span><span>.conf</span></span> (may require running <span><span>updatedb</span></span> first)
<span><span>find /home/lilo/ -type f ! -perm 755</span></span> Search for all files in <span><span>/home/lilo</span></span> that do not have permission 755
<span><span>find /home/lilo/ -type f -perm 777</span></span> Search for all files in <span><span>/home/lilo</span></span> that have permission 777
<span><span>ls -ltr</span></span> List all files in the current directory, sorted by access/creation time
<span><span>find /tmp/ -mmin -20</span></span> Find all files in <span><span>/tmp</span></span> that have been modified in the last 20 minutes
<span><span>find /tmp -iname file -exec chmod 777 {} \;</span></span> Search for files named <span><span>file</span></span> (case insensitive) and change their permissions to 777
<span><span>find /var/log/ -size 8k</span></span> Search for files in <span><span>/var/log</span></span> that are 8k in size
<span><span>find / * -perm +6000 -type f -exec ls -ld {} \; > setuid.txt</span></span> Create a list in <span><span>setuid.txt</span></span> containing the names of all binaries that have setuid and setgid set

3. Networking

Command Description
<span><span>curlftpfs ftp-user:[email protected] /mnt/my_ftp/</span></span> Mount a remote FTP server to the local file system <span><span>/mnt/my_ftp/</span></span>
<span><span>ssh [email protected] '( cd /tmp/ && touch ssh_file.txt )'</span></span> Remotely execute a command using <span><span>ssh</span></span>
<span><span>ssh [email protected] '( cat /etc/passwd )' > /tmp/passwd</span></span> Create a local copy of the remote <span><span>/etc/passwd</span></span> file
<span><span>airodump-ng -c 6 -w data-capture wlan0</span></span> Use the <span><span>wlan0</span></span> wireless interface to sniff wireless network packets
<span><span>macchanger -r eth0</span></span> Generate a random fake MAC address for the <span><span>eth0</span></span> network interface
<span><span>ssh -L 4500:127.0.0.1:23 haopython.com</span></span> Create an SSH tunnel for Telnet using local port 4500
<span><span>ssh -L 8025:mail.sample.org:25 mail.sample.org</span></span> Tunnel traffic from local system port 8025 to port 25 of <span><span>mail.sample.org</span></span>
<span><span>lsof -i tcp:22</span></span> Show services using port 22 (TCP)
<span><span>ethtool eth0</span></span> Show the status of the <span><span>eth0</span></span> network interface
<span><span>iwlist wlan0 scanning</span></span> Scan for available wireless networks using the <span><span>wlan0</span></span> interface
<span><span>netstat -ant</span></span> List all TCP ports on the system
<span><span>netstat -tupl</span></span> List all available services on the system
<span><span>ip route add default via 10.10.10.10</span></span> Set the default route via <span><span>10.10.10.10</span></span>

4. Arithmetic Operations and Conversions

Command Description
<span><span>echo $((0xFFF))</span></span> Convert a hexadecimal number (here FFF) to decimal (4095) using shell expansion
<span><span>echo $((8</span><span>#44</span><span>))</span></span> Convert an octal number (here 44) to decimal (36) using shell expansion
<span><span>echo "obase=16; ibase=10; 555;" | bc</span></span> Convert a decimal number (here 555) to hexadecimal using the <span><span>bc</span></span> command
<span><span>echo "obase=8; ibase=10; 64;" | bc</span></span> Convert a decimal number (here 64) to octal using the <span><span>bc</span></span> command
<span><span>echo "obase=16; ibase=8; 255;" | bc</span></span> Convert an octal number (here 255) to hexadecimal using the <span><span>bc</span></span> command
<span><span>echo "3447.2 * 343.61" | bc</span></span> Multiplication operation. Addition and subtraction use <span><span>+</span></span> and <span><span>-</span></span>
<span><span>echo "scale=10; 100 / 3" | bc</span></span> Division operation, floating-point precision of 10 digits
<span><span>units -t '13miles' 'km'</span></span> Convert miles to kilometers (e.g., 13 miles)
<span><span>units -t '10.5inches' 'cm'</span></span> Convert inches to centimeters (e.g., 10.5 inches)
<span><span>units -t '78344352ms' 'hour'</span></span> Convert milliseconds to hours

5. Text Processing

Command Description
<span><span>dd if=commands.txt of=commands.new conv=lcase</span></span> Convert all characters from uppercase to lowercase (does not modify the source file, creates a new file <span><span>commands.new</span></span>)
<span><span>rename 's/\.sh$/.bash/' *.sh</span></span> Rename all files with the extension <span><span>.sh</span></span> in the current working directory to <span><span>.bash</span></span>
<span><span>rename 's/^/new_/' *.conf</span></span> Add a prefix <span><span>new_</span></span> to all files with the extension <span><span>.conf</span></span> in the current working directory
<span><span>grep -v ^\# /etc/ntp.conf | grep .</span></span> Only display non-comment lines in the configuration file and ignore empty lines
<span><span>ls | grep " " | while read -r f; do mv "$f" \</span></span>echo $f | tr ‘ ‘ ‘_’`; done` Remove all spaces from filenames in the current working directory (replace with underscores)
<span><span>ls | while read -r f; do mv "$f" \</span></span>echo $f | tr ‘[A-Z]’ ‘[a-z]’`; done` Convert all filenames in the current directory from uppercase to lowercase

6. Date and Time

Command Description
<span><span>date -ud@1244763573</span></span> Convert epoch time to Coordinated Universal Time (UTC)
<span><span>date -d "Dec 23 18:10:02 EST 2010" +%s</span></span> Convert date to epoch time
<span><span>echo 'wget -c http://linux.com/distro.iso' | at 03:00</span></span> Download an ISO image at 3 AM (<span><span>-c</span></span> option allows resuming the download after a network interruption)
<span><span>date -d '2 Feb 2013' +%A</span></span> What day of the week is February 2, 2013? (…Saturday)
<span><span>units -t '10 days + 6 hours + 26 minutes + 59 seconds' 'seconds'</span></span> Convert time to seconds

7. Multimedia

Command Description
<span><span>wodim --devices</span></span> Get the block device filename of the CD burner
<span><span>cdrecord -v blank=all dev=/dev/scd0</span></span> Completely erase a CD-RW disc (Note: use <span><span>wodim –devices</span></span> to get the device filename)
<span><span>cdrecord -v blank=fast dev=/dev/scd0</span></span> Quickly erase a CD-RW disc (Note: use <span><span>wodim –devices</span></span> to get the device filename)
<span><span>ffmpeg -i out.wav -acodec libmp3lame out.mp3</span></span> Convert WAV audio format to MP3
<span><span>normalize-mp3 *.mp3</span></span> Normalize the volume of all MP3 audio files to reduce volume jumps between tracks
<span><span>cat file1.mp3 file2.mp3 > out.mp3</span></span> Merge all MP3 audio files into a single track
<span><span>sox file1.wav file2.wav file3.wav out.wav</span></span> Merge all WAV audio files into a single track
<span><span>for i in $( ls ); do ffmpeg -i $i $i.wav; done</span></span> Convert all MP3 or AC3 audio files to WAV format
<span><span>normalize-audio -m *.wav</span></span> Normalize the volume of all WAV audio files to reduce volume jumps between tracks
<span><span>cdrecord -v -nofix -eject dev='/dev/scd0' -audio -pad *.wav</span></span> Burn all WAV audio files to CD using <span><span>/dev/scd0</span></span> device
<span><span>cdrecord -v -fix -eject dev='/dev/scd0'</span></span> Close the CD session using the <span><span>/dev/scd0</span></span> burning device
<span><span>ffmpeg -f x11grab -s xga -r 25 -i :0 -sameq screen.mpg</span></span> Save screen recording to the <span><span>screen.mpg</span></span> video file
<span><span>for i in $( ls *.jpg ); do convert -resize 25% $i new_$i; done</span></span> Resize all images (here <span><span>*.jpg</span></span>) in the current directory to 25% of their original size
<span><span>mkisofs -o /tmp/cd.iso /path/to/your/files/</span></span> Create an ISO image from files in <span><span>/path/to/your/files/</span></span>
<span><span>wodim -eject -tao speed=0 dev=/dev/scd0 -v -data /my/image.iso</span></span> Burn an ISO image using <span><span>wodim</span></span> and <span><span>/dev/scd0</span></span> burning device
<span><span>mount -t iso9660 /path/to/iso/file.iso /mnt/iso -o loop</span></span> Mount an ISO image to the <span><span>/mnt/iso</span></span> directory
<span><span>xrandr --output VGA --auto</span></span> Clone video output to the VGA port (for presentations. Use <span><span>xrandr</span></span> without parameters to check if VGA is connected to a projector)
<span>arecord -d 10 /tmp/out.wav</span> Test the microphone

8. Disk Usage and Management

Command Description
<span><span>time dd if=/dev/hdb of=/dev/null bs=1024k</span></span> Non-destructive hard disk speed and size test (replace <span><span>/dev/hdb</span></span> with your hard disk)
<span><span>du -m --max-depth 1 | sort -rn | head -11</span></span> Get the size of all directories in the current working directory, sort and display the top 10 largest (Note: the first directory is the parent directory)
<span><span>du -s * | sort -k1,1rn | head</span></span> Display the top 10 largest files or directories in the current working directory
<span><span>dd if=/dev/zero of=/sp bs=10000 count=10000; mkswap /sp; swapon /sp</span></span> Create a file of size 100MB <span><span>/sp</span></span>, generate a swap signature and add the <span><span>/sp</span></span> file to the system’s total swap memory (this will add 100MB of swap space to the system)
<span><span>dpkg-query -Wf='${Installed-Size;10}\t${Package}\n' | sort -k1,1rn</span></span> Debian Package Management Only. Display all installed packages sorted by size from largest to smallest
<span><span>rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1rn</span></span> RPM Package Management Only. Display all installed packages sorted by size from largest to smallest
<span><span>head -c 100000000 /dev/urandom > file.data</span></span> Create a file containing random data, approximately 100MB in size
<span><span>dd bs=1 seek=2TB if=/dev/null of=~/large-file</span></span> Create a 2TB sparse file <span><span>~/large-file</span></span> (does not take up actual space)
<span><span>df -h .</span></span> Display free space information for the partition where the current working directory is located

9. Hardware Information

Command Description
<span><span>biosdecode</span></span> Retrieve BIOS information
<span><span>dmidecode -s bios-vendor</span></span> Retrieve your BIOS vendor information
<span><span>dmidecode --type baseboard</span></span> Retrieve your motherboard information
<span><span>ls -la /dev/disk/by-id/usb-*</span></span> USB disk device files (Note: USB disk must be plugged in. May not work on all systems)
<span><span>hdparm -I /dev/sdx</span></span> Model information of the hard disk <span><span>/dev/sdx</span></span>
<span><span>hdparm -tT /dev/sdx</span></span> Test the speed of the hard disk (Note: this test does not consider the file system)
<span><span>hddtemp /dev/sda</span></span> Check the temperature of the hard disk <span><span>/dev/sda</span></span>
<span><span>lspci | grep VGA</span></span> Get graphics card information
<span><span>dmidecode --type 4</span></span> Retrieve processor information (also try <span><span>cat /proc/cpuinfo</span></span>)
<span><span>x86info -a 2> /dev/null | grep Connector | uniq</span></span> Retrieve processor socket type (requires <span><span>x86info</span></span> command, may need to install <span><span>x86info</span></span> package)
<span><span>dmidecode -t 17</span></span> Detect the number of used RAM slots, their speed, and size (also try: <span><span>lshw -C memory -short</span></span>)
<span><span>cat /dev/sndstat</span></span> Check sound card settings and the modules in use
<span><span>powersave -b</span></span> Get battery information
<span><span>free -m</span></span> Check the free memory of the system (including swap memory). Other options: <span><span>top</span></span>, <span><span>cat /proc/meminfo</span></span>
<span><span>fdisk -l | grep GB</span></span> Check the size of all disks (including USB disks)

10. Other Tips

Command Description
<span><span>head -c 4 /dev/urandom | mimencode</span></span> Generate 8 random characters (Note: <span><span>mimencode</span></span> is part of the <span><span>metamail</span></span> package)
<span><span>echo "DISPLAY=$DISPLAY xmessage -center 'abc'" | at "NOW +1hour"</span></span> Display a GUI message in the center of the screen one hour later
<span><span>:(){ :|:& };:</span></span> Fork Bomb. A simple way to crash the system (Warning: Do not try this on a production system!)
<span><span>ccrypt mypasswords.txt</span></span> Encrypt a file
<span><span>ccdecrypt mypasswords.txt.cpt</span></span> Decrypt a file previously encrypted with <span><span>ccrypt</span></span>

Quick Reference Guide to Common Linux Commands

Leave a Comment