Linux Commands

$ ls -las # list directory contents

$ touch aa.txt ## create an empty file

$ mv aa.txt bb.txt # rename file

$ rm -i aa.txt # confirm and remove file

$ rm -r example 

$ mkdir # make directory

$ cd /home/user # absolute path

$ cd test/ # relative path

chown and chmod

$ chown user:group demo.txt

example is chown john:users demo.txt

chmod command samples

Give full access to user and group (i.e read, write and execute ) on a specific file.

$ chmod ug+rwx file.txt

Revoke all access for the group (i.e read, write and execute ) on a specific file.

$ chmod g-rwx file.txt

Apply the file permissions recursively to all the files in the sub-directories.

$ chmod -R ug+rwx file.txt

$ chmod 400 aa.txt

$ chmod +x aa.txt

tar command samples

$ tar cvf archive_name.tar dirname/

$ tar xvf archive_name.tar

grep command samples

$ grep -i “the” demo_file Search for a given string in a file (case in-sensitive search).

$ grep -A 3 -i “example” demo_text Print the matched line, along with the 3 lines after it.

$ grep -r “bread” * Search for a given string in all files recursively

find command samples

$ find -iname “notes.txt”

Execute commands on files found by the find command

$ find -iname “notes.txt” -exec md5sum {} ;

Find all empty files in home directory

$ find ~ -empty

sed command samples

This example converts the DOS file format to Unix file format using sed command.

seds/.//’ filename

Print file content in reverse order

$ sed -n ‘1!G;h;$p’ thegeekstuff.txt

Add line number for all non-empty-lines in a file

$ sed ‘/./=’ thegeekstuff.txt | sed ‘N; s// /’

awk command samples

Remove duplicate lines using awk

$ awk ‘!($0 in array) { array[$0]; print }’ temp

Print all lines from /etc/passwd that has the same uid and gid

$awk -F ‘:’ ‘$3==$4’ passwd.txt

Print only specific field from a file.

$ awk ‘{print $2,$5;}’ employee.txt

service command samples

$ service ssh status

$ service --status-all

$ service ssh restart

top command samples

$ top

$ top -u testuser

Cron command samples

View crontab entry for a specific user

$ crontab -u john -l

Schedule a cron job every 10 minutes.

/10 * * * /home/john/check-disk-space

This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.

30 08 10 06 * /home/john/full-backup

00 11,16 * * * /home/ramesh/bin/incremental-backup

00 – 0th Minute (Top of the hour) 11,16 – 11 AM and 4 PM * – Every day * – Every month * – Every day of the week

9 to 6 pm every week day

00 09-18 * * * /home/john/bin/check-db-status

9 to 6 pm Monday to Friday

00 09-18 * * 1-5 /home/ramesh/john/check-db-status

$ crontab -l # list crontab entries

$ crontab -e # edit crontab entries

MySql command samples

To connect to a remote mysql database. This will prompt for a password.

$ mysql -u root -p -h

To connect to a local mysql database.

$ mysql -u root -p

Yum command samples

To install apache using yum.

$ yum install httpd

To upgrade apache using yum.

$ yum update httpd

To uninstall/remove apache using yum.

$ yum remove httpd

rpm command samples

To install apache using rpm.

$ rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm

To upgrade apache using rpm

$ rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm

To uninstall/remove apache using rpm

$ rpm -ev httpd

apt command samples

$ apt update

$ apt install packagename

$ apt remove packagename

Other Commands

$ df
$ free 
$ kill
$ ps
$ whereis
$ which python
$ whatis ls
$ locate crontab
$ tail /var/log/nginx/access.log
$ less /var/log/nginx/error.log
$ ping
$ date
$ wget 
$ curl
The tee command in UNIX is a command line utility for copying standard input to standard output. It supports writing whatever it is given from standard input to standard output and optional writing to one or more files.

echo 'foo' | tee foo.txt
cat foo.txt