Select Page

List of common shell commands for Linux

Trying to find the right command for your Linux  Box on the internet is time consuming. I often find myself looking for the same commands again and again. To fix this, I am trying to create a list of most commonly used Linux commands for my self that is easily accessible. I will keep adding to this list over the time and hope fully it will keep expanding and will benefit some of you as well.

 

Copy files and folders

cp -option source target

Copy a directory to another directory
cp -r /home/www/public_html/test /home/www/public_html/test2/

Copy all the files from a directory to another directory
cp -r /home/www/public_html/test/* /home/www/public_html/test2/

 

Move files and folders with mv

mv -option source target

moving multi-folder
mv folder1 folder2 target

moving folder and a file
mv folder1 file1 target

use mv -i option to prompt if a file already exists
mv -i folder1 file1 target

 

Zip and Unzip

Install
yum install zip unzip

To compress a file
zip – r zipFileName.zip fileOrDirName

extract the contents of a zip file
unzip filename.zip

You can use zip to compress multiple files and directories at the same time by listing them with a space between each one.
zip to compress multiple files and directories at the same time
zip -r filename.zip file1 file2 file3 /usr/work/school

 

Remove Directory

Removes the directory and all of its contents
rm -rf dirName

 

Find Linux Distribution Name and Version

cat /etc/*-release

 

Create RSA SSH key

ssh-keygen -t rsa

 

Change directory and file permissions

Change permission of the directory and all sub dirctories
find dir-name -type d -exec chmod 755 {} \;

Change permission of all files in the directory and all sub-directories
find file-name -type f -exec chmod 644 {} \;

 

Check directory and file size

Check size of a specific directory
du -sh /home/foldername/

Check size of all the files and sub-directories in a directory
du -sh /home/foldername/*

 

Check harddrive size

Check size of all the partitions
df -h

 

Find Linux Distribution Name and Version

cat /etc/*-release

 

Check version details of installed programs/packages

For Redhat/CentOS/Fedora/Suse
rmp -qi {packagename}

For Ubuntu/Debian
dpkg -p {packagename}

For Ubuntu/Debian
dpkg -p {packagename}

 

 

User Management

 

Add existing user to a group

Create user ‘demo’ 
adduser demo

Create user ‘demo’  with a home directory specified
useradd -d /srv/www/public_html/demo demo

Change user’s home directory 
usermod -m -d /srv/www/public_html/demo demo

Add user ‘demo’ to www-data group
usermod -a -G www-data demo

Remove user ‘demo’ from www-data group
gpasswd -d  demo www-data

Test if the user has been added to the group or not
id demo
groups demo