Common Linux Commands

Command Code Description
arch Show processor architecture of the machine (1)
uname -m Show processor architecture of the machine (2)
uname -r Show the kernel version in use
dmidecode -q Display hardware system components - (SMBIOS / DMI)
hdparm -i /dev/hda List a disk's architecture features
hdparm -tT /dev/sda Perform a test read operation on the disk
cat /proc/cpuinfo Display CPU info
cat /proc/interrupts Show interrupts
cat /proc/meminfo Check memory usage
cat /proc/swaps Show which swap areas are in use
cat /proc/version Show the kernel version
cat /proc/net/dev Show network adapters and statistics
cat /proc/mounts Show mounted filesystems
lspci -tv List PCI devices
lsusb -tv Show USB devices
date Show system date
cal 2007 Show calendar for the year 2007
date 041217002007.00 Set date and time - month day hour minute year. second
clock -w Save the time modification to BIOS

Command Code Description
shutdown -h now Power off the system
init 0 Power off the system
telinit 0 Power off the system
shutdown -h hours:minutes & Power off the system at a scheduled time
shutdown -c Cancel scheduled shutdown
shutdown -r now Restart
reboot Restart
logout Logout

Command Code Description
cd /home Change to '/home' directory'
cd .. Go back to the previous directory
cd ../.. Go back two directories
cd Change to your home directory
cd ~user1 Change to the user's home directory
cd - Return to the last directory
pwd Show current working path
ls View files in the directory
ls -F View files in the directory
ls -l Show detailed information about files and directories
ls -a Show hidden files
ls *[0-9]* Show filenames and directory names containing numbers
tree Show the file and directory tree starting from root
lstree Show the file and directory tree starting from root
mkdir dir1 Create a directory named 'dir1'
mkdir dir1 dir2 Create two directories simultaneously
mkdir -p /tmp/dir1/dir2 Create a directory tree
rm -f file1 Delete a file named 'file1'
rmdir dir1 Delete a directory named 'dir1'
rm -rf dir1 Delete a directory named 'dir1' and its contents
rm -rf dir1 dir2 Delete two directories and their contents simultaneously
mv dir1 new_dir Rename/Move a directory
cp file1 file2 Copy a file
cp dir/* . Copy all files from a directory to the current working directory
cp -a /tmp/dir1 . Copy a directory to the current working directory
cp -a dir1 dir2 Copy a directory
ln -s file1 lnk1 Create a soft link to a file or directory
ln file1 lnk1 Create a hard link to a file or directory
touch -t 0712250000 file1 Modify the timestamp of a file or directory - (YYMMDDhhmm)
iconv -l List known encodings
iconv -f fromEncoding -t toEncoding inputFile > outputFile Change character encoding
find . -maxdepth 1 -name *.jpg -print -exec convert Batch resize files in current directory and send to thumbnail directory (requires ImageMagick conversion)

Command Code Description
find / -name file1 Search for files and directories starting from the root '/'
find / -user user1 Search for files and directories owned by 'user1'
find /home/user1 -name \*.bin Search for files ending with '.bin' in the '/home/user1' directory
find /usr/bin -type f -atime +100 Search for executable files that have not been used in the last 100 days
find /usr/bin -type f -mtime -10 Search for files that have been created or modified within the last 10 days
find / -name \*.rpm -exec chmod 755 '{}' \; Search for files ending with '.rpm' and set their permissions
find / -xdev -name \*.rpm Search for files ending with '.rpm', ignoring removable devices like CD-ROMs and USB drives
locate \*.ps Find files ending with '.ps' - must run 'updatedb' command first
whereis halt Display the location of a binary file, source code, or man page
which halt Display the full path of a binary or executable file

Command Code Description
mount /dev/hda2 /mnt/hda2 Mount a disk named hda2 - ensure the directory '/mnt/hda2' already exists
umount /dev/hda2 Unmount a disk named hda2 - first exit from the mount point '/mnt/hda2'
fuser -km /mnt/hda2 Force unmount when the device is busy
umount -n /mnt/hda2 Run the unmount operation without writing to the /etc/mtab file - very useful when the file is read-only or when the disk is full
mount /dev/fd0 /mnt/floppy Mount a floppy disk
mount /dev/cdrom /mnt/cdrom Mount a cdrom or dvdrom
mount /dev/hdc /mnt/cdrecorder Mount a cdrw or dvdrom
mount /dev/hdb /mnt/cdrecorder Mount a cdrw or dvdrom
mount -o loop file.iso /mnt/cdrom Mount a file or ISO image file
mount -t vfat /dev/hda5 /mnt/hda5 Mount a Windows FAT32 filesystem
mount /dev/sda1 /mnt/usbdisk Mount a USB drive or flash device
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share Mount a Windows network share

Command Code Description
df -h Show a list of mounted partitions
ls -lSr |more List files and directories sorted by size
du -sh dir1 Estimate disk space used by directory 'dir1'
du -sk * | sort -rn List the size of files and directories in order of capacity
rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n List the space used by installed rpm packages based on size (for Fedora, Redhat systems)
dpkg-query -W -f='${Installed-Size;10}t${Package}n' | sort -k1,1n List the space used by installed deb packages based on size (for Ubuntu, Debian systems)

Command Code Description
groupadd group_name Create a new user group
groupdel group_name Delete a user group
groupmod -n new_group_name old_group_name Rename a user group
useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 Create a user belonging to the "admin" group
useradd user1 Create a new user
userdel -r user1 Delete a user ('-r' excludes home directory)
usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes
passwd Change password
passwd user1 Change a user's password (only allowed for root)
chage -E 2020-12-31 user1 Set the expiration date for user password
pwck Check the format and syntax of the '/etc/passwd' file and existing users
grpck Check the format and syntax of the '/etc/group' file and existing groups
newgrp group_name Log in to a new group to change the default group of newly created files

Command Code Description
ls -lh Display permissions
ls /tmp | pr -T5 -W$COLUMNS Divide terminal into 5 columns
chmod ugo+rwx directory1 Set the permissions to read (r), write (w), and execute (x) for the owner (u), group (g), and others (o) on a directory
chmod go-rwx directory1 Remove read, write, and execute permissions for group (g) and others (o) on a directory
chown user1 file1 Change the ownership of a file
chown -R user1 directory1 Change the ownership of a directory and simultaneously change the attributes of all files in that directory
chgrp group1 file1 Change the group of a file
chown user1:group1 file1 Change the ownership and group attributes of a file
find / -perm -u+s List all files in the system that have the SUID bit set
chmod u+s /bin/file1 Set the SUID bit of a binary file - the user running the file is granted the same permissions as the owner
chmod u-s /bin/file1 Disable the SUID bit of a binary file
chmod g+s /home/public Set the SGID bit of a directory - similar to SUID, but for directories
chmod g-s /home/public Disable the SGID bit of a directory
chmod o+t /home/public Set the STICKY bit of a file - only allow the legitimate owner to delete the file
chmod o-t /home/public Disable the STICKY bit of a directory

Command Code Description
chattr +a file1 Only allow appending read and write to the file
chattr +c file1 Allow this file to be automatically compressed/decompressed by the kernel
chattr +d file1 Ignore this file when performing file system backups with the dump program
chattr +i file1 Set the file as immutable, cannot be deleted, modified, renamed, or linked
chattr +s file1 Allow a file to be securely deleted
chattr +S file1 Once an application writes to this file, the system will immediately write the changes to the disk
chattr +u file1 If the file is deleted, the system will allow you to recover the deleted file later
lsattr Display special attributes

Command Code Description
bunzip2 file1.bz2 Unzip a file named 'file1.bz2'
bzip2 file1 Compress a file named 'file1'
gunzip file1.gz Unzip a file named 'file1.gz'
gzip file1 Compress a file named 'file1'
gzip -9 file1 Maximum compression
rar a file1.rar test_file Create a compressed file named 'file1.rar'
rar a file1.rar file1 file2 dir1 Compress 'file1', 'file2', and directory 'dir1' simultaneously
rar x file1.rar Unzip rar archive
unrar x file1.rar Unzip rar archive
tar -cvf archive.tar file1 Create a non-compressed tarball
tar -cvf archive.tar file1 file2 dir1 Create an archive file containing 'file1', 'file2', and 'dir1'
tar -tf archive.tar Show contents of an archive
tar -xvf archive.tar Extract an archive
tar -xvf archive.tar -C /tmp Extract an archive to the /tmp directory
tar -cvfj archive.tar.bz2 dir1 Create a bzip2 format compressed archive
tar -jxvf archive.tar.bz2 Unzip a bzip2 format compressed archive
tar -cvfz archive.tar.gz dir1 Create a gzip format compressed archive
tar -zxvf archive.tar.gz Unzip a gzip format compressed archive
zip file1.zip file1 Create a zip format compressed archive
zip -r file1.zip file1 file2 dir1 Compress several files and directories into a zip format archive
unzip file1.zip Unzip a zip format archive

Command Code Description
rpm -ivh package.rpm Install an rpm package
rpm -ivh --nodeps package.rpm Install an rpm package ignoring dependency warnings
rpm -U package.rpm Update an rpm package without changing its configuration files
rpm -F package.rpm Update a sure installed rpm package
rpm -e package_name.rpm Remove an rpm package
rpm -qa Show all installed rpm packages on the system
rpm -qa | grep httpd Show all installed rpm packages that contain "httpd" in their names
rpm -qi package_name Get special information about an installed package
rpm -qg "System Environment/Daemons" Show the rpm package of a component
rpm -ql package_name Show the list of files provided by an installed rpm package
rpm -qc package_name Show the list of configuration files provided by an installed rpm package
rpm -q package_name --whatrequires Show a list of dependencies for an rpm package
rpm -q package_name --whatprovides Show the size occupied by an rpm package
rpm -q package_name --scripts Show the scripts executed during installation/removal
rpm -q package_name --changelog Show modification history for an rpm package
rpm -qf /etc/httpd/conf/httpd.conf Verify which rpm package provides the given file
rpm -qp package.rpm -l Show the list of files provided by an rpm package that is not yet installed
rpm --import /media/cdrom/RPM-GPG-KEY Import public key digital certificate
rpm --checksig package.rpm Verify the integrity of an rpm package
rpm -qa gpg-pubkey Verify the integrity of all installed rpm packages
rpm -V package_name Check file size, permission, type, owner, group, MD5 checksum, and last modification time
rpm -Va Check all installed rpm packages on the system - use with caution
rpm -Vp package.rpm Verify that an rpm package is not yet installed
rpm2cpio package.rpm | cpio --extract --make-directories *bin* Run executable files from an rpm package
rpm -ivh /usr/src/redhat/RPMS/`arch`/package.rpm Install a constructed package from rpm source
rpmbuild --rebuild package_name.src.rpm Build an rpm package from rpm source

Command Code Description
yum install package_name Download and install an rpm package
yum localinstall package_name.rpm Install an rpm package, resolving all dependencies using your repository
yum update package_name.rpm Update all installed rpm packages in the current system
yum update package_name Update an rpm package
yum remove package_name Remove an rpm package
yum list List all packages installed on the current system
yum search package_name Search for a package in the rpm repository
yum clean packages Clean rpm cache by removing downloaded packages
yum clean headers Remove all header files
yum clean all Remove all cached packages and header files

Command Code Description
dpkg -i package.deb Install/Update a deb package
dpkg -r package_name Remove a deb package from the system
dpkg -l Show all installed deb packages on the system
dpkg -l | grep httpd Show all deb packages containing "httpd" in their names
dpkg -s package_name Get special information about a specific package installed in the system
dpkg -L package_name Show the list of files provided by a deb package installed on the system
dpkg --contents package.deb Show the list of files provided by a package that is not yet installed
dpkg -S /bin/ping Verify which deb package provides the given file

Command Code Description
apt-get install package_name Install/Update a deb package
apt-cdrom install package_name Install/Update a deb package from a CD
apt-get update Upgrade the list of packages
apt-get upgrade Upgrade all installed software
apt-get remove package_name Remove a deb package from the system
apt-get check Verify if the dependencies of the software repository are correct
apt-get clean Clean the cache from downloaded software packages
apt-cache search searched-package Return package names that contain the search string

Command Code Description
cat file1 View the content of the file starting from the first byte
tac file1 View the content of the file starting from the last line
more file1 View the content of a long file
less file1 Similar to 'more' command, but allows backward movement in the file as well
head -2 file1 View the first two lines of a file
tail -2 file1 View the last two lines of a file
tail -f /var/log/messages Real-time view of content being added to a file

Command Code Description
cat file1 | command( sed, grep, awk, grep, etc...) > result.txt Merge detailed explanation text of a file and write the summary to a new file
cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt Merge detailed explanation text of a file and append the summary to an existing file
grep Aug /var/log/messages Search for the keyword "Aug" in the file '/var/log/messages'
grep ^Aug /var/log/messages Search for words that start with "Aug" in the file '/var/log/messages'
grep [0-9] /var/log/messages Select all lines in the '/var/log/messages' file that contain numbers
grep Aug -R /var/log/* Search for the string "Aug" in the '/var/log' directory and its subdirectories
sed 's/stringa1/stringa2/g' example.txt Replace "string1" with "string2" in example.txt
sed '/^$/d' example.txt Delete all blank lines from example.txt
sed '/ *#/d; /^$/d' example.txt Delete all comments and blank lines from the file
echo 'example' | tr '[:lower:]' '[:upper:]' Merge cell contents
sed -e '1d' result.txt Exclude the first line from example.txt
sed -n '/stringa1/p' View only lines that contain the word "string1"
sed -e 's/ *$//' example.txt Remove trailing whitespace from each line
sed -e 's/stringa1//g' example.txt Remove the word "string1" from the document and keep the rest
sed -n '1,5p;5q' example.txt View the content from the first line to the fifth line
sed -n '5p;5q' example.txt View the fifth line
sed -e 's/00*/0/g' example.txt Replace multiple zeros with a single zero
cat -n file1 Number the lines of the file
cat example.txt | awk 'NR%2==1' Remove all even lines from example.txt
echo a b c | awk '{print $1}' View the first column of a line
echo a b c | awk '{print $1,$3}' View the first and third columns of a line
paste file1 file2 Combine the contents of two files or columns
paste -d '+' file1 file2 Combine the contents of two files or columns separated by a "+"
sort file1 file2 Sort the contents of two files
sort file1 file2 | uniq Get the union of two files (duplicate lines retain only one)
sort file1 file2 | uniq -u Remove the intersection, leaving the other lines
sort file1 file2 | uniq -d Get the intersection of two files (only keep files that exist in both files)
comm -1 file1 file2 Compare the contents of two files and remove only content contained in 'file1'
comm -2 file1 file2 Compare the contents of two files and remove only content contained in 'file2'
comm -3 file1 file2 Compare the contents of two files and remove the common parts

Command Code Description
dos2unix filedos.txt fileunix.txt Convert the file format from MSDOS to UNIX
unix2dos fileunix.txt filedos.txt Convert the file format from UNIX to MSDOS
recode ..HTML < page.txt> page.html Convert a text file to html
recode -l | more Display all allowed conversion formats

Command Code Description
badblocks -v /dev/hda1 Check for bad blocks on disk hda1
fsck /dev/hda1 Repair/Check the integrity of the Linux filesystem on disk hda1
fsck.ext2 /dev/hda1 Repair/Check the integrity of the ext2 filesystem on disk hda1
e2fsck /dev/hda1 Repair/Check the integrity of the ext2 filesystem on disk hda1
e2fsck -j /dev/hda1 Repair/Check the integrity of the ext3 filesystem on disk hda1
fsck.ext3 /dev/hda1 Repair/Check the integrity of the ext3 filesystem on disk hda1
fsck.vfat /dev/hda1 Repair/Check the integrity of the fat filesystem on disk hda1
fsck.msdos /dev/hda1 Repair/Check the integrity of the dos filesystem on disk hda1
dosfsck /dev/hda1 Repair/Check the integrity of the dos filesystem on disk hda1

Command Code Description
mkfs /dev/hda1 Create a filesystem on partition hda1
mke2fs /dev/hda1 Create a Linux ext2 filesystem on partition hda1
mke2fs -j /dev/hda1 Create a Linux ext3 (journaling) filesystem on partition hda1
mkfs -t vfat 32 -F /dev/hda1 Create a FAT32 filesystem
fdformat -n /dev/fd0 Format a floppy disk
mkswap /dev/hda3 Create a swap filesystem

Command Code Description
mkswap /dev/hda3 Create a swap filesystem
swapon /dev/hda3 Enable a new swap filesystem
swapon /dev/hda2 /dev/hdb3 Enable two swap partitions

Command Code Description
dump -0aj -f /tmp/home0.bak /home Create a full backup of the '/home' directory
dump -1aj -f /tmp/home0.bak /home Create an interactive backup of the '/home' directory
restore -if /tmp/home0.bak Restore an interactive backup
rsync -rogpav --delete /home /tmp Synchronize directories on both sides
rsync -rogpav -e ssh --delete /home ip_address:/tmp rsync over SSH
rsync -az -e ssh --delete ip_addr:/home/public /home/local Synchronize a remote directory to a local directory over SSH and with compression
rsync -az -e ssh --delete /home/local ip_addr:/home/public Synchronize a local directory to a remote directory over SSH and with compression
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' Perform a backup of the local disk on a remote host via SSH
dd if=/dev/sda of=/tmp/file1 Backup disk content to a file
tar -Puf backup.tar /home/user Perform an interactive backup of the '/home/user' directory
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p' Copy content of a directory to a remote directory via SSH
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' Copy content of a local directory to a remote directory via SSH
tar cf - . | (cd /tmp/backup ; tar xf - ) Locally copy a directory to another place, preserving original permissions and links
find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents Find and copy all files ending with '.txt' from one directory to another
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2 Find all files ending with '.log' and create a bzip package
dd if=/dev/hda of=/dev/fd0 bs=512 count=1 Perform