Basic Linux commands (CentOS 6/7)

Below we provide frequently used commands to work with Linux (CentOS) server

System Information

Show computer architecture:

arch

Show kernel version

uname -r

Show system date

date

Show calendar table for 2014

cal 2014

Files and Directories

Go to '/var/www/'

cd /var/www

Go to the upper level directory

cd ..

Go to home directory

cd ~

Go to home directory of “user”

cd ~user

Show the contents of the current directory

ls

Show the contents of the current directory adding characters to the names characterizing the type

ls -F

Show detailed representation of files and directories in the current directory

ls -l

Show hidden files and directories in the current directory

ls -a

Create a directory called ‘test’

mkdir test

Create two directories simultaneously

mkdir test1 test2

Create a directory tree

mkdir -p /var/www/test/dir

Delete the file named ‘file’

rm -f file

Delete a directory named ‘dir’

rmdir dir

Delete a directory named ‘dir’ and all of its contents recursively

rm -rf dir

Rename or move a file or directory

mv dir1 new_dir

Copy file1 in file2

cp file1 file2

Create a symbolic link to a file or directory

ln -s file1 file2

Searching Files

Find files and directories named file1. Start searching from the root (/)

find / -name file1

Find files and directories owned by user1. Start searching from the root (/)

find / -user user1

Find all files and directories whose names end in ‘.log’. Start searching from ‘/var/www’

find /var/www -name "*.log"

Find all files containing '.png' in the name. it is recommended to run 'updatedb' command before this.

locate "*.png"

Find all files with '.log' extension in the current directory, including subdirectories and delete them

find . -name '*.log' -type f -delete

Disk Space

Information about partitioned sections displaying total, free and used space

df -h

Show the size used by directory ‘dir1’

du -sh dir1

Users and Groups

Create user1, assign /home/user1 as its home directory, assign /bin/bash as a shell, include it in ‘admin’ group and add a comment Nome Cognome

useradd -c "Nome Cognome" -g admin -d /home/user1 -s /bin/bash user1

Create user1

useradd user1

Delete user1 and its home directory

userdel -r user1

Create a new group named group_name

groupadd group_name

Delete group group_name

groupdel group_name

Rename group old_group_name in new_group_name

groupmod -n new_group_name old_group_name

Change password

passwd

Change password of user1 (only root)

passwd user1

Setting/Changing File Rights

Add rights 777 (Read Write Execute) to directory1 – full rights to everybody.

chmod 777 directory1

Add rights to directory1, including all files and subfolders in it, rights 777 (Read Write Execute) - full rights to everybody.

chmod –R 777 directory1

Assign user1 as the owner of file1

chown user1 file1

Recursively assign user1 as the owner of directory1

chown -R user1 directory1

Assign user 'apache' from the group 'apache' to folder 'dir', including all subfolders and files:

chown apache:apache -R /var/www/dir

Find all files in the current directory, including subfolders and assign rights 664

find . -type f -printf "\"%p\" " | xargs chmod 664

Find all folders in the current directory, including subfolders and assign rights 775

find . -type d -printf "\"%p\" " | xargs chmod 775

File Archiving and Compression

Unarchive file 'file1.bz2'

bunzip2 file1.bz2

Unarchive file 'file1.gz'

gunzip file1.gz

Archive file 'file1' to file1.gz

gzip file1

Archive file file1 to file1.bz2

bzip2 file1

Create an archive and zip it with gzip

tar -cvfz archive.tar.gz dir1

Unarchive and extract

tar -xvfz archive.tar.gz

Create zip-archive

zip file1.zip file1

Unarchive and extract zip-archive

unzip file1.zip

Upgrading Packages

Upload and install package

yum install package_name

Update all packages installed in the system

yum update

Update package

yum update package_name

Delete package

yum remove package_name

Show the list of all packages installed in the system

yum list

Find a package in the repository

yum search package_name

Clean rpm-cache, deleting uploaded packages

yum clean packages

Clean rpm-cache, deleting uploaded packages and headers

yum clean all