Basic Linux Commands — Linux commands for day to day use

UNIX – It originally began as a propriety operating system from Bell Laboratories, which later spawned into different commercial versions.

Linux – It is free, open source and intended as a non-propriety operating system for the masses. It’s an operating system based on UNIX, and was first introduced by Linus Torvalds.

Kernel – The Linux Kernel is a low-level systems software whose main role is to manage hardware resources for the user. It is also used to provide an interface for user-level interaction.

Shell – It is the utility that processes user’s requests. When we type in a command at our terminal, the shell interprets the command and calls the program that we want. Various types of shells which are available with most Unix variants are BASH, C Shell, Bourne Shell and Korn Shell.

BASH — BASH is short for Bourne Again SHell. It was written by Steve Bourne as a replacement to the original Bourne Shell (represented by /bin/sh). It combines all the features from the original version of Bourne Shell, plus additional functions to make it easier and more convenient to use. It has since been adapted as the default shell for most systems running L,nux.

Files and Directories – Data’s of Unix is organized into file and those files are stored into folder/directories. These directories are further organized into a tree-like structure called the file system.

Commands – While working on shell, we need to use commands to use it process our requests. Examples of various commands are –  ls, mkdir, cp, mv, cat, grep, etc. There are around 250 standard commands plus numerous others provided through 3rd party software. All the commands come along with various options.

You can get access to a Linux machine by following below tutorial on google cloud set up and creating machine/instance from google cloud.

— Google Cloud set-up

— Create Instance / Machine in Google Cloud Platform (GCP)

Login to system and then we can try various Linux commands.

>> echo $SHELL — command to find out shell one is using

>>who — by this command we can see the user name and their ip addresses who have logged in to our server.

>> whoami — this command shows current logged in terminal user name

>> who am i (with spaces ) — this command shows you the logged in terminal number and user name and more detailed information.

>> date: displays the current date

date +”%D %H:%M:%S”

Options:

d – The da of the month (1-31)

y – The last two digits of the year

H,M,S – Hour Minute and second respectively

D – the date in mm/dd/yy

>> cal Command to see calender for any specific month or a complete year

>> top : Displays CPU processes in a full-screen GUI. A great way to see the activity on your computer in real-time. Type “q” to quit

>>Uptime : show online summary of system status

>>history: It’s a very useful command which list previously executed commands

>>info : Enter this command to list the various commands and its documentation.

>>man: show on-line documentation by program name

Once manual of a command is opened, you can Type “q” to quit

>> which  Shows the full path of shell commands found in your path. For example, if you want to know exactly where the “grep” command is located on the filesystem, you can type :

“which grep”. The output should be something like:

/bin/grep

>> whereis It Locates the program, source code, and manual page for a command (if at all information is available).

For example, to find out where “ls” and its man page are, type: “whereis ls” The output will look something like: ls: /bin/ls /usr/share/man/man1/ls.1.gz

>> locate A quick way to search for files anywhere on the filesystem. For example, you can find all files and directories that contain the name “mozilla” by typing: locate mozilla

>> touch command is the easiest way to create new, empty files.

touch file1 file2 file3

>> cat command allows us to view content of file. “cat” commands can be also used to concatenate files and redirect output in terminal or files.

cat abc (where abc is filename)

cat abc any_filename

cat abc any_filename > new_file

>> cp– This command is used to copy files

cp abc1.txt abc2.txt ( It will copy content of a file abc1.txt to abc2.txt)

cp abc1.txt /home/techie/work/ ( This command will copy abc1.txt file inside work folder)

>> ls : list files in a directory and their attributes

>> mv Move command is used to change file name and also to move content to different directory location

>> rm : This command is used to remove files

rm filename ( removes file)

rm -rf filename (forcefully removes files)

rm -rf * ( forcefully removes entire files in the present directory)

>> ln : This command create link ( name) to file :

There are two types of links :

Symlink Link and Hard Link : Both are pointers to files; the difference is the kind of pointer.

A hardlink can only work on the same filesystem, it is simply a different name for the same inode (files are internally referenced by inodes). Hardlinks usually only work for files, not directories.

ln filename.txt abc.link here abc.link is the hard link to the file “filename.txt”

A symlink (symbolic link) is a special file containing a path to another file. This path can be absolute or relative. symlinks can work across file systems, and can even point to different files, if you for example unplug an external hard drive and replace it with another one, which has a different file at the same path. A symlink can point to either files or directories.

ln -s filename.txt abc.symlink

>> chmod : (change file permissions)

The chmod command is used to change access permissions to files and directories.

The format is chmod permissions filename

chmod 755 file.txt

>> To see what permissions a file or directory has in linux, you use the ls command with option -l (eg: ls -l) which gives a long format listing.

techiematerials@rishu-linux:~/test$ ls -l

-rw-rw-r– 2 techiematerials techiematerials 0 Apr 17 11:40 abc.link

lrwxrwxrwx 1 techiematerials techiematerials 12 Apr 17 11:41 abc.symlink -> filename.txt

-rw-rw-r– 2 techiematerials techiematerials 0 Apr 17 11:40 filename.txt

The permissions are the first 10 characters of the line (-rwxrwx—) and can be broken down as follows.

rwx

(4+2+1)

r—

(4+2+1)

r—

(4+2+1)

1 root root 765 Apr 23 file.txt
File type Owner Group All Links Owner Group Size Mod date Filename

The r,w and x stand for…

  • r = read
  • w = write
  • x = execute

The first character on the line shows what type of file or directory it is, and can be one of these things…

  • = file
  • d = directory
  • l = symbolic link
  • b = block-type special file
  • c = character-type special file
  • p = named pipe
  • S = socket
  • s = XENIX semaphore
  • m = XENIX shared data (memory) file
  • D = Solaris door
  • n = HP-UX network special file

The remaining 9 characters are broken down into 3 groups of 3 characters. The first three are the permissions for the owner, the middle three are permissions for the group which has access to the file and the last three are the permissions for everybody else.

>> usermod  – This command is used to modify the user profile

>> find  –  A very powerful command, but sometimes tricky to use. It can be used to search for files matching certain patterns, as well as many other types of searches.

A simple example is:

find . -name \*mp3

This example starts searching in the current directory “.” and all subdirectories, looking for files with “mp3” at the end of their names.

find / -name filename  to find any file or directory in linux file system

find . -type f -name “*.txt”  to find files with “.txt” at the end of their names

find /jenkins/jobs/ -type f -name "config.xml" -exec cp --parent -rf {} ~/jobs_backup \; this command will copy entire config.xml file from location “/jenkins/jobs” to ~/jobs_backup location.

  • Write a command that will look for files with an extension “c”, and has the occurrence of the string “apple” in it.

Answer: Find . -name “*.c” | xargs grep –i “apple”

  • Write a command that will do the following:
    -look for all files in the current and subsequent directories with an extension c,v
    -strip the,v from the result (you can use sed command)
    -use the result and use a grep command to search for all occurrences of the word ORANGE in the files.

Find ./ -name “*.c,v” | sed ‘s/,v//g’ | xargs grep “ORANGE”

 

>> foreach

icdcmopt02s {ambinteg} foreach i (`cat /home/ambinteg/sushil/IOTA02`)

foreach? echo $i

foreach? /grid/common/bin/rsync -azv –progress  –rsync-path=/grid/common/bin/rsync icmnet03:/icd/sockits/$i/ /icd/sockits/$i/

foreach? end

>> diff — Command to find difference between two files

$ diff filename1 filename2 (It compare two files for differences )

$vimdiff filename1 filename2 “OR” sdiff filename1 filename2

It will display the two files side by side

>> head  This command display content of files.  It shows first few lines of a file (ex:   head -5 filename)

>> tail    This command also display content of files but it show last few lines of a file (ex:    tail -10 filename)

>> grep  It display line that match the pattern (ex:   grep good filename)

>> pr  It format file with page headers, multiple column etc.

>> od  This display binary file as equivalent octal/hex codes

>> file  It examine files and tell you weather text, data etc

>> more and less commands are used to view large files one page at a time

less is the standard pager for linux and in general less is more powerful than more.

>> cat /proc/cpuinfo Displays information about your CPU.

>> cat /proc/meminfo Display lots of information about current memory usage.

>> wc [options] [file name]  command is used to count lines, words and characters, depending on the option used.

$ wc sample1.txt

65 2776 17333 sample1.txt

Which means sample1.txt file has 65 lines, 2776 words, and 17333 characters

We can just print number of lines, number of words or number of characters by using following options:

-l : Number of lines

-w : Number of words

-c : Number of characters

To find number of content of folder we can use command :  ls foldername |wc –l

>>cmp command is used to compare two files whether they are identical or not

>> useradd  Use this command to create a new user

[root@techie root]# useradd username

>> passwd This command is used to give a password of a user

[root@techie root]# passwd rishu

>> userdel  This command is used to remove a user from linux

[root@techie root]# userdel rishu

>> groupadd  to add a new group

[root@techie root]# groupadd groupname

>> groupdel  to delete a group

[root@techie root]#groupdel groupname

>> chown —to change the ownership of a file or directory

[root@techie root]# chown ownername filename

chown samuel abcd

>> chgrp to change the group ownership of a file or directory

[root@techie root]#chgrp newgroupownername filename

chgrp samuel abcd

>> ps –u username Command to displays the process of the specified user

>> ps –aux  This command displays all processes including user and system process

$ps –aux |grep “user1”  Displays all processes including user and system process and grep for the process running by user – “user1”

ps -ef  Command that display entire processes running in the system:

ps -ef | grep java   Command to list entire java process

>> lsof  : use lsof to see what application is listening on any port.

ex- use this cmd to check which app is listening on port 80:

sudo lsof -i TCP:80

>> df   This command report file system disk space usage

df -h .   This command list current disk space usage

>> du –sh abc.txt  This command displays abc.txt file size

du   This command show disk usage in a directory.

du -s  This command provides a summary for the current directory.

>> free   This command displays amount of free and used memory in the system.

>> uname -a This command prints system information to the screen (kernel version, machine type, etc.)

>> Command to control programe execution in Shell

  • &              — run job in foreground
  • DEL,^c   — kill job in foreground
  • ^z            — suspend job in foreground
  • fg             — restart suspended job in foreground
  • bg            — run suspended job in background
  • ;               — delimit command on same line
  • ()             — group command on same line
  • !               — re-run earlier command from history list
  • nice         — run program at lower priority
  • at             — run program at later time
  • ps             — Lists currently running process (programs).

 

>> crontab

export EDITOR=vi ;to specify a editor to open crontab file.

  • crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
  • crontab -l      Display your crontab file.
  • crontab -r      Remove your crontab file.

Crontab file syntax :

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

* in the value field above means all legal values as in braces for that column.

The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

Notes

A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed.

Crontab Example

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

min hour day/month month day/week Execution time
30 0 1 1,6,12 * – 00:30 Hrs  on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * – midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 – At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

Crontab Environment:

cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).

cron supplies a default environment for every shell, defining:

HOME=user’s-home-directory

LOGNAME=user’s-login-id

PATH=/usr/bin:/usr/sbin:.

SHELL=/usr/bin/sh

 

  • Disable Email
    By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

/dev/null 2>&1

 

>> telnet  This command enables remote network login to other computer

>> ftp  This command enable network file transfer program

>> rlogin  remote login to “trusted“ computer

>> rsh execute single command on remote trusted computer

>> rcp remote file copy from/to “trusted “ computer

>> host find ip address for given host name, or vice versa

Network Addresses: The IP address of a host consists of two parts, network address and host address.

The network address will be same for all nodes in the network.

The host address is unique and it is for the host only. Example: 182.198.20.150

Here the network address is 182.198.20 and the host address is 150.

>> ifconfig  It sets the IP address and the subnet mask of the interface.

It also used to activate and deactivate an interface

syntax:$ ifconfig

>> netstat   netstat command is used to check the connectivity of the network.

Commad: $ netstat –rn

>> gzip  command is used to compress the file, and gunzip is used to de-compress it.

usage: gzip <file name>

It provides the extension .gz and removes the original file. The compression ratio depends on the type, size and nature of the file.

>> gunzip <file name with.gz>  This command decompress a file

 

gunzip sample_copy.txt.gz

If you want to compress the directory contents recursively, use -r option with gzip command

>> tar command is used to create archive that contains a group or file or entire directory structure.

It is generally used for back ups.

usage: tar [options] <output file.tar> <file1 or dir> . . .

The following are the options:

  • -c Create an archive
  • -x Extract files from archive
  • -t Display files in archive
  • -f arch Name the archive arch

Cmd:-

$ tar -cvf compression.tar compression

$ gzip compression.tar

$ //will create compression.tar.gz file

>> For un-compression the file first use gunzip command, which will create a tar file and then use tar command to untar the contents

$ gunzip compression.tar.gz

$ tar -xvf compression.tar

To just view the contents of the tar file use -t option

$ tar -tvf compression.tar

>> Instead of doing tar first and then gzip next, we can combine both using the option -z

tar -cvzf compression.tar.gz compression

>> We can de-compress .tar.gz again in a single command using the option -z with -x

tar -xvzf compression.tar.gz

>> zip : command can be used for archiving as well as compressing the contents of the directory or the file

usage: zip [options] output.zip <files to be zipped or directory>

$ zip sample1.zip sample1.txt

Use -r option to recursively zip the contents of the directory

$ zip -r compression.zip compression

// will create compression.zip file

>> To un-compress the file use unzip command

unzip compression.zip

// will un-compress the compression.zip file

>>Controlling programe input/output for c-shell

  • |             – pipe output to input
  • >            – redirect output to a storage file
  • <            – redirect input from a storage file
  • >>         – append redirected output to storage file
  • script    – make file record of all terminal activity
  • tee         – Sends the output in two directions at a time

Syntax: tee [options ] filename .

Ex : $ls –l |tee file2

It will list all the files in the current directory and as well stores in the file called file2.

$ls –l |tee –a file2

It will append the list to file2.

>> nmap –  Nmap short form of Network Mapper is an open source tool for  exploring networks, perform security scans, network audit and finding open ports on remote machine.

Use below command to install nmap :

# yum install nmap                                                  [on Red Hat based systems]
$ sudo apt-get install nmap                                   [on Debian based systems]

 

nmap command to find all ports being used in a machine/server :

sudo nmap -sT -O localhost

 

 

>> sed   – This command works by sequentially reading a file into memory and then performs the

specified action. It is used for  substitution.

sed ‘s/hi/hello/’ filename  This will display the file called filename after the substitution.

sed -i ‘s/hello/hi/’ filename This command will do substitution(insert) in the file “filename”.

sed ‘s/hi/hello/g’ filename — This command will make Global replacement .. i.e if it will replace all the “hi” to “hello” ( Without “g” option it will just replace first “hi” of a line to “hello”)

“e” is used for multiple Substituions

sed -e ‘s/hi/hello/g’ -e ‘s/fine/good/g’ filename

 

Eg-1. How to remove the headers from a file using command in Linux?

sed ‘1 d’ file.txt

The only problem with above command is that, it outputs the file on standard output without the first line. In order to save the output to file, we need to use redirect operator which will redirects the output to a file.

sed ‘1 d’ file.txt > new_file.txt

Well the built in switch ‘-i‘ for sed command, can perform this operation without a redirect operator.

sed -i ‘1 d’ file.txt

Eg-2. How will you check the length of a line from a text file?

A ‘sed –n ‘n p’ file.txt‘, where ‘n‘ represents the line number and ‘p‘ print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. So, how to get the length count? Obviously! we need to pipeline the output with ‘wc‘ command.

# sed –n ‘n p’ file.txt | wc –c

To get the length of line number ‘5’ in the text file ‘tecmint.txt‘, we need to run.

# sed -n ‘5 p’ tecmint.txt | wc -c

Eg-3. Given a file, replace all occurrence of word “ABC” with “DEF” from 5th line till end in only those lines that contains word “MNO”

sed –n ‘5,$p’ file1|sed ‘/MNO/s/ABC/DEF/’

Eg-4. Print the 10th line without using tail and head command.

sed –n ‘10p’ file1

Eg-5. 38: How do we delete all blank lines in a file?

sed ‘^ [(backslash)011(backslash)040]*$/d’ file1

where (backslash)011 is octal equivalent of space and

(backslash)040 is octal equivalent of tab

Eg-6. How will I insert a line “ABCDEF” at every 100th line of a file?

sed ‘100i\ABCDEF’ file1

 

>>AWK  is a programming language. You can write awk scripts for complex operations or you can use awk from the command line. Just as sed, awk reads one line at a time, performs some action depending on the condition you give it and outputs the result.

Example : Command using “awk” to get a list of the installed software in a machine:

$ dpkg -l | awk ' {print $2} ' > software_intalled

note: The above command will store the output in a file “software_intalled”

 

The action to be performed by awk is enclosed in braces, and the whole command is quoted. But the syntax is :

awk ' condition { action }'

In our example, we had no condition, but if we wanted to, say, check only for python packages installed (yes, we can also use grep, but this is an example, plus why to use two utilities when you can only use one?), we would have done this:

$ dpkg -l | awk ' /'python'/ {print $2} '

 

Command using awk to print just the file names in a folder :

ll | awk '{print $9}'

 

>>nohup — when using command shell, if we prefix nohup, it prevents the command from being aborted even if we log out or exit the shell. nohup stands for “no hangup”.

syntax :

nohup commands-to-be-executed &

Example :

nohup sh customScript.sh &

nohup /path/to/script/CustomScript.pl arg1 arg2 &

The “&” symbol at the end runs commands in the background, returning us to the command prompt while it is running.

It is good to use nohup in conjunction with & if we want to continue running other commands.

 

>> Rsync stands for “remote sync”. It is a tool by which we can sync data’s (files/folders) locally as well as remotely. With the help of rsync command you can copy and sync data’s r across directories, disks and networks and can perform data backups between two linux machines.

Syntax :

# rsync options source destination

Some options used with rsync command:

-v : verbose

-r : copies data recursively (but it doesn’t preserve timestamps and permission while transferring data)

-a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps

-z : compress file data

-h : human-readable, output numbers in a human-readable format

 

To install rsync :

# yum install rsync (On Red Hat based systems)

# apt-get install rsync (On Debian based systems)

 

Example 1. : Sync two directories in a local server

rsync -zvr /var/www/html/techie/ /root/temp

 

Example 2. Synchronize Only One File

rsync -v /var/www/html/techie/conf.txt /root/temp/

 

Example 3. Synchronize Files From Local to Remote

rsync -avz /var/www/html/techie/ adminuser@192.157.100.14:/home/temp/techie/

(You will asked to enter password of remote server)

 

Example 4. Synchronize Files From Remote to Local

rsync -avz adminuser@192.157.100.14:/var/www/html/techiem/ /root/temp

(You will asked to enter password of remote server)

 

What is mounting

You’re probably a bit confused at first: you put your floppy or CD into the drive and start wondering why you’re not able to access it! This is because your floppies, CDs, hard disk partitions, and other storage devices must be attached to some existing directory on your system before they can be accessed. This attaching is called mounting, and the directory where the device is attached is called a mount point.

After the device is mounted, you can access the files on that device by accessing the directory where the device is attached. When you’re done and want to remove the floppy or CD or other device, you need to detach, unmount, it before removing it.

 How to mount

Mounting is done with the mount command.When mounting, you must tell the mount command what is the device or partition you want to mount and what is the mount point. The mount point must be a directory that already exists on your system. For example, to mount your floppy:
$ mount /dev/fd0 /mnt/floppy

How to unmount

Unmounting is done with the umount command. No, I didn’t make a typo: the command really is umount, not unmount. When unmounting, you’ll need to tell umount what mounted device to unmount, either by telling what’s the device or the mount point. For example, if /dev/fd0 is mounted to /mnt/floppy, you’ll unmount it with
$ umount /mnt/floppy
or
$ umount /dev/fd0It’s not wise to remove the floppy from the floppy drive without unmounting it first! In the worst case the data you were writing to the floppy wasn’t written into it yet. With CD-ROMs you can’t do this: the tray won’t even open if you haven’t unmounted the CD first

 

Leave a Reply

Your email address will not be published. Required fields are marked *