Linux Basics
Linux Basics
redirectio
-Jatin Maheshwari
chown command in Linux with Examples
Different users in the operating system have ownership and permission to ensure that the files are
secure and put restrictions on who can modify the contents of the files. In Linux there are different
users who use the system:
• Each user has some properties associated with them, such as a user ID and a home directory. We
can add users into a group to make the process of managing users easier.
• A group can have zero or more users. A specified user can be associated with a “default group”. It
can also be a member of other groups on the system as well.
Ownership and Permissions: To protect and secure files and directory in Linux we use permissions to
control what a user can do with a file or directory. Linux uses three types of permissions:
• Read: This permission allows the user to read files and in directories, it lets the user read
directories and subdirectories stores in it.
• Write: This permission allows a user to modify and delete a file. Also it allows a user to modify
its contents (create, delete and rename files in it) for the directories. Unless the execute
permission is not given to directories changes does do affect them.
• Execute: The write permission on a file allows it to get executed. For example, if we have a file
named php.sh so unless we don’t give it execute permission it won’t run.
Types of file Permissions:
• User: These type of file permission affect the owner of the file.
• Group: These type of file permission affect the group which owns the file. Instead of the group
permissions, the user permissions will apply if the owner user is in this group.
• Other: These type of file permission affect all other users on the system.
Note: To view the permissions we use:
ls -l
Now if I use file1.txt in my case, to change ownership I will use the following syntax:
chown master file1.txt
where the master is another user in the system. Assume that if you are user named user1 and you want
to change ownership to root (where your current directory is user1). use “sudo” before syntax.
sudo chown root file1.txt
Options:
• -c: Reports when a file change is made.
Example:
chown -c master file1.txt
• -v: It is used to show the verbose information for every file processed.
Example:
chown -v master file1.txt
• -f: It supresses most of the error messages. When you are not permitted to change group
permissions and shows error, this option forcefully/silently changes the ownership.
Examples:
1. To Change group ownership In our case I am using group1 as a group in the system. To
change ownership we will use
chown :group1 file1.txt
You can see that the group permissions changed to group1 from root, if you use -v option it will
report that. We just need to add a “:” to change group.
2. To change the owner as well as group: Again taking master as user and group1 as a group in the
system
chown master:group1 greek1
3. To change the owner from particular ownership only: Suppose we want to change ownership
from master to root where current owner must be master only.
chown --from=master root greek1
In Unix-like operating systems, the chmod command is used to change the access mode of a file.
The name is an abbreviation of change mode.
Syntax :
chmod [reference][operator][mode] file...
The references are used to distinguish the users to whom the permissions apply i.e. they are list of
letters that specifies whom to give permissions. The references are represented by one or more of the
following letters:
The operator is used to specify how the modes of a file should be adjusted. The following operators are
accepted:
Operator Description
classes
Note : Putting blank space(s) around operator would make the command fail.
The modes indicate which permissions are to be granted or removed from the specified classes. There
are three basic modes which correspond to the basic permissions:
Let us take a look at above figure. To make things easy to understand some columns and rows are
eliminated and extra spaces are added to the permissions column to make it easier to read as shown
below:
... so on...
•The very first column represents the type of the file i.e. is it a normal file or a
directory where d represents a directory and – represents a normal file.
•The first set three letters after the file type tell what the Owner of the file, have permissions to
do. For example: In assgn1_client.c, has owner’s permission as rw-, which means the owner mik
can only read(r) and write(w) the file but cannot execute(x).
•Note: The 3rd and 4th columns represents the name of the owner of the file and the group to
which the owner belongs respectively.
•The next three letters after the user’s permission are the group’s permissions.
For example: header.sh has group permissions as r-x, which means Other people in the mik group
can not write(w) the header.sh script but can only read(r) or execute(x) it.
•Note that when a directory has the x set, this takes the special meaning of “permitted to search
this directory”.
•The last three letters in the permissions column tell us what the “others” may do. The general
practice is to protect the files from external access so that others can’t write any files or
directories. They may read(r) or execute(x) it. For example: The assgn1_client.c has others
permission as r- – which means it can only be read by other(external) access but cannot be written
or executed by them.
Now, let us see how chmod command can be used to change the access mode of a file.
Example 1 :
Let’s change the assgn1_client.c permission so that the owner cannot write(w) in the file but can only
read it.
Example 2 :
Let’s restrict the permission such that the user cannot search the directory EXAM.
After applying the chmod u=rw EXAM command, the user (owner) cannot change the directory. If the
user tries to change the directory, then it shows the message “Permission denied” as shown in the figure
below :
octal notations
First of all it is very essential to know about ugo and rwx manner:
• Understanding of attribute which can be out put by ls-l:
• Applying Permission:
This direct connection between commands/ programs/ processes allows them to operate simultaneously
and permits data to be transferred between them continuously rather than having to pass it through
temporary text files or through the display screen.
Pipes are unidirectional i.e data flows from left to right through the pipeline.
Syntax :
command_1 | command_2 | command_3 | .... | command_N
Example :
1. Listing all files and directories and give it as input to more command.
$ ls -l | more
The more command takes the output of $ ls -l as its input. The net effect of this command is that the
output of ls -l is displayed one screen at a time. The pipe acts as a container which takes the output of ls
-l and gives it to more as input. This command does not use a disk to connect standard output of ls -l to
the standard input of more because pipe is implemented in the main memory.
In terms of I/O redirection operators, the above command is equivalent to the following command
sequence.
$ ls -l -> temp
[contents of temp]
rm temp
2. Use sort and uniq command to sort a file and print unique values.
$ sort record.txt | uniq
This will sort the given file and print the unique values only.
This command select first 7 lines and last 5 lines from the file and print those lines which are common
to both of them.
4. Use ls and find to list and print all lines matching a particular pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;
This command select files with .txt extension in the given directory and search for pattern like
“program” in the above example and print those ine which have program in them.
5. Use cat, grep, tee and wc command to read the particular entry from user and store in a file
and print line count.
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l
This command select Rajat Dua and store them in file2.txt and print total number of lines
matching Rajat Dua
Output Redirection
The '>' symbol is used for output (STDOUT) redirection.
Example:
ls -al > listings
Here the output of command ls -al is re-directed to file "listings" instead of your screen.
Note: Use the correct file name while redirecting command output to a file. If there is an existing file
with the same name, the redirected command will delete the contents of that file and then it may be
overwritten."
If you do not want a file to be overwritten but want to add more content to an existing file, then you
should use '>>' operator.
You can redirect standard output, to not just files, but also devices!
$ cat music.mp3 > /dev/audio
The cat command reads the file music.mp3 and sends the output to /dev/audio which is the audio
device. If the sound configurations in your PC are correct, this command will play the file music.mp3
Input redirection
The '<' symbol is used for input(STDIN) redirection
Example: The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device keyboard. But if you want to attach a
File to email you can use the input re-direction operator in the following format.
Mail -s "Subject" to-address < Filename
This would attach the file with the email, and it would be sent to the recipient.
The above examples were simple. Let's look at some advance re-direction techniques which make use
of File Descriptors.
Error Redirection
Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard
input, standard output, standard error.
These files are always present whenever a program is run. As explained before a file descriptor, is
associated with each of these files.
File File Descriptor
Standard Input STDIN 0
Standard Output STDOUT 1
Standard Error STDERR 2
By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other
than the screen.
Why Error Redirection?
Error re-direction is one of the very popular features of Unix/Linux.
Frequent UNIX users will reckon that many commands give you massive amounts of errors.
•For instance, while searching for files, one typically gets permission denied errors. These
errors usually do not help the person searching for a particular file.
•While executing shell scripts, you often do NOT want error messages cluttering up the normal
program output.
The solution is to re-direct the error messages to a file.
Example 1
$ myprogram 2>errorsfile
Summary
•Each file in Linux has a corresponding File Descriptor associated with it
•The keyboard is the standard input device while your screen is the standard output device
•">" is the output redirection operator. ">>" appends output to an existing file
•"<" is the input redirection operator
•">&"re-directs output of one file to another.
•You can re-direct error using its corresponding File Descriptor 2.
mkdir, rmdir, touch, cat, tail , more, rm, mv,
cp, cd, wget, links
-Teertha
mkdir :
mkdir command in Linux allows the user to create directories (also referred to as folders in some
operating systems ).
This command can create multiple directories at once as well as set the permissions for the directories.
Options:
--version: It displays the version number, some information regarding the license and exits.
-p: A flag which enables the command to create parent directories as necessary. If the directories
exist, no error is specified.
-m: This option is used to set the file modes, i.e. permissions, etc. for the created directories. The
syntax of the mode is the same as the chmod command.
rmdir
rmdir command is used remove empty directories from the filesystem in Linux.
The rmdir command removes each and every directory specified in the command line only if these
directories are empty.
So if the specified directory has some directories or files in it then this cannot be removed by rmdir
command.
rmdir -p: In this option each of the directory argument is treated as a pathname of which all
components will be removed, if they are already empty, starting from the last component.
rmdir -v, –verbose: This option displays verbose information for every directory being processed.
rmdir –ignore-fail-on-non-empty: This option do not report a failure which occurs solely
because a directory is non-empty. Normally, when rmdir is being instructed to remove a non-
empty directory, it simply reports an error. This option consists of all those error messages.
rmdir –version: This option is used to display the version information and exit.
touch
The touch command is a standard command used in UNIX/Linux operating system which is used to
create, change and modify timestamps of a file. Basically, there are two different commands to create a
file in the Linux system which is as follows:
touch command: It is used to create a file without any content. The file created using touch command
is empty. This command can be used when the user doesn’t have data to store at the time of file
creation.
Touch command Syntax to create a new file: You can create a single file at a time using touch
command.
Touch command to create multiple files: Touch command can be used to create the multiple numbers
of files at the same time. These files would be empty while creation.
options:
touch -a: This command is used to change access time only. To change or update the last access or
modification times of a file touch -a command is used.
Syntax: touch -a fileName
touch -c : This command is used to check whether a file is created or not. If not created then don’t
create it. This command avoids creating files.
touch -m : This is used to change the modification time only. It only updates last modification time.
touch -r : This command is used to use the timestamp of another file. Here Doc2 file is updated with
the time stamp of File 1.
cat
Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their
content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat
commands.
4) Create a file
7) Cat command can append the contents of one file to the end of another file.
Output :Will append the contents of one file to the end of another file
8) Cat command can display content in reverse order using tac command.
Tail:
The tail command, as the name implies, print the last N number of data of the given input.
If more than one file name is provided then data from each file is precedes by its file name.
Syntax:
tail [OPTION]... [FILE]...
1. -n num: Prints the last ‘num’ lines instead of last 10 lines. numis mandatory to be
specified in command otherwise it displays an error. This command can also be written
as without symbolizing ‘n’ character but ‘-‘ sign is mandatory.’
$ tail -n 3 state.txt
$ tail -3 state.txt
Tail command also comes with an ‘+’ option which is not present in the head
command. With this option tail command prints the data starting from specified line
number of the file instead of end. For command: tail +n file_name, data will start
printing from line number ‘n’ till the end of the file specified.
2. -c num: Prints the last ‘num’ bytes from the file specified. Newline count as a single
character, so if tail prints out a newline, it will count it as a byte. In this option it is
mandatory to write -cfollowed by positive or negative num depends upon the
requirement. By +num, it display all the data after skipping numbytes from starting of
the specified file and by -num, it display the last num bytes from the file specified.
Note: Without positive or negative sign before num, command will display the
last num bytes from the file specified.
3. -q: It is used if more than 1 file is given. Because of this command, data from each file is not
precedes by its file name.
4. -f: This option is mainly used by system administration to monitor the growth of the log
files written by many Unix program as they are running. This option shows the last ten
lines of a file and will update when new lines are added. As new lines are written to the
log, the console will update with the new lines. The prompt doesn’t return even after
work is over so, we have to use the interrupt key to abort this command. In general, the
applications writes error messages to log files. You can use the -foption to check for
the error messages as and when they appear in the log file.
More
more command is used to view the text files in the command prompt, displaying one screen at a
time in case the file is large (For example log files). The more command also allows the user do
scroll up and down through the page. The syntax along with options and command is as follows.
Another application of more is to use it with some other command after a pipe. When the output is
large, we can use more command to see output one by one.
Syntax:
more [-options] [-num] [+/pattern] [+linenum] [file_name]
[-options]: any option that you want to use in order to change the way the file is displayed.
Choose any one from the followings: (-d, -l, -f, -p, -c, -s, -u)
[-num]: type the number of lines that you want to display per screen.
[+/pattern]: replace the pattern with any string that you want to find in the text file.
[+linenum]: use the line number from where you want to start displaying the text content.
[file_name]: name of the file containing the text that you want to display on the screen.
While viewing the text file use these controls:
Enter key: to scroll down line by line.
Space bar: To go to the next page.
b key: To go to back one page.
Options:
-d : Use this command in order to help the user to navigate. It displays “[Press space to continue, ‘q’ to
quit.]” and displays “[Press ‘h’ for instructions.]” when wrong key is pressed.
-f : This option does not wrap the long lines and displays them as such.
-p : This option clears the screen and then displays the text.
-c : This command is used to display the pages on the same area by overlapping the previously displayed text.
-s : This option squeezes multiple blank lines into one single blank line.
+/pattern : This option is used to search the the string inside your text document. You can view all the instances
by navigating through the result.
+num : This option displays the text after the specified number of lines of the document.
rm
rm stands for remove here. rm command is used to remove objects such as files, directories,
symbolic links and so on from the file system like UNIX. To be more precise, rm removes
references to objects from the filesystem, where those objects might have had multiple references
(for example, a file with two different names). By default, it does not remove directories.
This command normally works silently and you should be very careful while running rm command
because once you delete the files then you are not able to recover the contents of files and
directories.
Syntax:
rm [OPTION]... FILE...
Options:
1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for
confirmation before removing each file, you have to press y for confirm deletion, any other key
leaves the file un-deleted.
2. -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f option
overrides this minor protection and removes the file forcefully.
3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the
files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds.
Normally, rm wouldn’t delete the directories but when used with this option, it will delete.
4. –version: This option is used to display the version of rmwhich is currently running on your system.
mv:
mv stands for move. mv is used to move one or more files or directories from one place to another in file system
like UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
No additional space is consumed on a disk during renaming. This command normally works silently means no
prompt for confirmation.
Syntax: mv [Option] source destination
mv a.txt geek.txt
If the destination file doesn’t exist, it will be created. In the above command mv simply replaces the
source filename in the directory with the destination filename(new name). If the destination file exist,
then it will be overwrite and the source file will be deleted.
Options:
1. -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation before
moving a file that would overwrite an existing file, you have to press y for confirm moving, any other
key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move
it to new location.
2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write protected.
The -f option overrides this minor protection and overwrite the destination file forcefully and delete the
source file.
3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.
4. -b(backup): With this option it is easier to take a backup of an existing file that will be overwritten as a
result of the mv command. This will create a backup file with the tilde character(~) appended to it.
5. –version: This option is used to display the version of mv which is currently running on your system.
cp:
cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of
a file on a disk with different file name. cp command require at least two filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
1. Two file names : If the command contains two file names, then it copy the contents of 1st
file to the 2nd file. If the 2nd file doesn’t exist, then first it creates one and content is copied to
it. But if it existed then it is simply overwritten without any warning. So be careful when you
choose destination file name.
cp Src_file Dest_file
2. One or more arguments : If the command has one or more arguments, specifying file names and
following those arguments, an argument specifying directory name then this command copies each
source file to the destination directory with the same name, created if not existed but if already existed
then it will be overwritten, so be careful !!.
3. Two directory names : If the command contains two directory names, cp copies all files of the
source directory to the destination directory, creating any files or directories needed. This mode
of operation requires an additional option, typically R, to indicate the recursive copying of
directories.
cp -R Src_directory Dest_directory
In the above command, cp behavior depend upon whether Dest_directory is exist or not. If
the Dest_directory doesn’t exist, cp creates it and copies content of Src_directoryrecursively as
it is. But if Dest_directory exists then copy of Src_directory becomes sub-directory
under Dest_directory.
Options:
1. -i(interactive): i stands for Interactive copying. With this option system first warns the user before
overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and
with any other option leave it uncopied.
2. -b(backup): With this option cp command creates the backup of the destination file in the same folder
with the different name and in different format.
3. -f(force): If the system is unable to open destination file for writing operation because the user doesn’t
have writing permission for this file then by using -f option with cp command, destination file is deleted
first and then copying of content is done from source to destination file.
4. -r or -R: Copying directory structure. With this option cpcommand shows its recursive behavior by
copying the entire directory structure recursively.
5. -p(preserve): With -p option cp preserves the following characteristics of each source file in the
corresponding destination file: the time of the last data modification and the time of the last access, the
ownership (only if it has permissions to do this), and the file permission-bits.
cd:
cd command in linux known as change directory command. It is used to change current working
directory.
Syntax: cd [directory]
To move inside a subdirectory : to move inside a subdirectory in linux we use
cd [directory_name]
cd /: this command is used to change directory to the root directory, The root directory is the
first directory in your filesystem hierarchy.
Wget
Wget is the non-interactive network downloader which is used to download files from the server even
when the user has not logged on to the system and it can work in the background without hindering the
current process.
GNU wget is a free utility for non-interactive download of files from the Web. It
supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.
wget is non-interactive, meaning that it can work in the background, while the user is
not logged on. This allows you to start a retrieval and disconnect from the system,
letting wget finish the work. By contrast, most of the Web browsers require constant
user’s presence, which can be a great hindrance when transferring a lot of data.
wget can follow links in HTML and XHTML pages and create local versions of remote
web sites, fully recreating the directory structure of the original site. This is sometimes
referred to as recursive downloading. While doing that, wget respects the Robot
Exclusion Standard (/robots.txt). wget can be instructed to convert the links in
downloaded HTML files to the local files for offline viewing.
wget has been designed for robustness over slow or unstable network connections; if a
download fails due to a network problem, it will keep retrying until the whole file has
been retrieved. If the server supports resuming, it will instruct the server to continue the
download from where it left off.
Syntax : wget [option] [URL]
Options :
1. -v / –version : This is used to display the version of the wget available on your system.
Syntax : wget -v
2. -h / –help : This is used to print a help message displaying all the possible options of the line command that is
available with the wget command line options
Syntax :wget -h [URL]
3. -o logfile : This option is used to direct all the messages generated by the system to the logfile specified by the
option and when the process is completed all the messages thus generated are available in the log file. If no log
file has been specified then the output messages are redirected to the default log file i.e. wget -log
Syntax: wget -o logfile [URL]
4. -b / –background : This option is used to send a process to the background as soon as the process has started
so that other processes can be carried out. If no output file is specified via the -o option, output is redirected to
wget-log by default.
Syntax: wget -b [URL]
5. -a : This option is used to append the output messages to the current output log file without overwriting the
file as in -o option the output log file is overwritten but by using this option the log of the previous command is
saved and the current log is written after that of the previous ones.
Syntax :wget -a logfile [URL]
6. -i : This option is used to read URLs from file. If -i is specified as file, URLs are read from the standard
input.If this function is used, no URLs need be present on the command line. If there are URLs both on the
command line and in an input file, those on the command lines will be the first ones to be retrieved. The file
need not be an HTML document if the URLs are just listed sequentially.
Syntax :wget -i inputfile
7. -t number / –tries=number : This option is used to set number of retries to a specified number of times.
Specify 0 or inf for infinite retrying. The default is to retry 20 times, with the exception of fatal errors like
connection refused or link not found, which are not retried once the error has occurred.
Syntax : wget -t number [URL]
8. -c : This option is used to resume a partially downloaded file if the resume capability of the file is yes
otherwise the downloading of the file cannot be resume if the resume capability of the given file is no or not
specified.
Syntax :wget -c [URL]
9. -w : This option is used to set the system to wait the specified number of seconds between the retrievals. Use
of this option is recommended, as it lightens the server load by making the requests less frequent. Instead of in
seconds, the time can be specified in minutes using the m suffix, in hours using h suffix, or in days using d
suffix. Specifying a large value for this option is useful if the network or the destination host is down, so that
wget can wait long enough to reasonably expect the network error to be fixed before the retry.
Syntax :wget -w number in seconds [URL]
10. -r : this option is used to turn on the recursive retrieving of the link specified in case of fatal errors also. This
option is a recursive call to the given link in the command line.
Syntax: wget -r [URL]
Links
A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are
pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links
allow more than one file name to refer to the same file, elsewhere.
For example, if we have a file a.txt. If we create a hard link to the file and then delete the file, we can
still access the file using hard link. But if we create a soft link of the file and then delete the file, we
can’t access the file through soft link and soft link becomes dangling. Basically hard link increases
reference count of a location while soft links work as a shortcut (like in Windows)
1. Hard Links
Each hard linked file is assigned the same Inode value as the original, therefore they reference the same
physical file location. Hard links more flexible and remain linked even if the original or linked files are
moved throughout the file system, although hard links are unable to cross different file systems.
ls -l command shows all the links with the link column shows number of links.
Removing any link, just reduces the link count, but doesn’t affect other links.
If original file is removed then the link will still show the content of the file.
A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft
linked file contains a separate Inode value that points to the original file. As similar to hard links, any
changes to the data in either file is reflected in the other. Soft links can be linked across different file
systems, although if the original file is deleted or moved, the soft linked file will not work correctly
(called hanging link).
ls -l command shows all links with first column value l? and the link points to original file.
Soft Link contains the path for original file and not the contents.
Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling” link
which points to non existent file.
Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft
links.
ls -a In Linux, hidden files start with . (dot) symbol and they are not visible in the
regular directory. The (ls -a) command will enlist the whole list of the current directory
including the hidden files.
ls -l It will show the list in a long list format.We will be able to view the file details
like type ,size , modified date etc.
Column 6 shows th date and time on which the file was recently modified.
ls -lh
This command will show you the file sizes in human readable format. Size of the file is
very difficult to read when displayed in terms of byte. The (ls -lh)command will give
you the data in terms of Mb, Gb, Tb, etc.
ls -d */
With this you can exclude column of group information and owner.
ls -ltr
It will sort the list by displaying recently modified filed at top and in reverse output
order.
Description : The who command is used to get information about currently logged in
user on to system.
Syntax : $who [options] [filename] The who command displays the following
information for each user currently logged in to the system if no option is provided :
Conventionally, seven runlevels exist, numbered from zero to six.Servers with GUI and
desktop start at runlevel 5. Linux servers which lack GUI start at runlevel 3.
top command is used to show the Linux processes. It provides a dynamic real-time view
of the running system.
Usually, this command shows the summary information of the system and the list of
processes or threads which are currently managed by the Linux Kernel.
As soon as you will run this command it will open an interactive command mode where
the top half portion will contain the statistics of processes and resource usage.
And Lower half contains a list of the currently running processes. Pressing q will simply
exit the command mode.
PID: Shows task’s unique process id.
PR: Stands for priority of the task.
SHR: Represents the amount of shared memory used by a task.
VIRT: Total virtual memory used by the task.
USER: User name of owner of task.
%CPU: Represents the CPU usage.
TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through
hundredths of a second.
SHR: Represents the Shared Memory size (kb) used by a task.
NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and
positive Nice value means lower priority.
%MEM: Shows the Memory usage of task.
ps command in Linux
As we all know Linux is a multitasking and multi-user systems. So, it allows multiple
processes to operate simultaneously without interfering with each other. Process is one
of the important fundamental concept of the Linux OS. A process is an executing
instance of a program and carry out different tasks within the operating system.
Linux provides us a utility called ps for viewing information related with the processes
on a system which stands as abbreviation for “Process Status”
Syntax –ps [options]
ps Simple process selection : Shows the processes for the current shell
Result contains four columns of information.
Where,
PID – the unique process ID
TTY – terminal type that the user is logged into
TIME – amount of CPU in minutes and seconds that the process has been running
CMD – name of the command that launched the process.
ps -A or ps -e View Processes : View all the running processes use either of the
following option with ps
ps -a View Processes not associated with a terminal : View all processes except both
session leaders and processes not associated with a terminal.
-q Use this option if you want to suppress the progress meter and non-error messages.
-C. This option will force scp to compresses the data as it is sent to the destination machine.
Copy a File Between Two Remote Systems using the scp Command:
scp user1@host1.com:/files/file.txt user2@host2.com:/files
SFTP
SFTP, which stands for SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol
packaged with SSH that works in a similar way over a secure connection. The advantage is the ability to
leverage a secure connection to transfer files and traverse the filesystem on both the local and remote system.
We can establish an SSH connection and then open up an SFTP session using that connection by
issuing the following command:
sftp sammy@your_server_ip_or_remote_hostname
The most useful command to learn first is the help command. This gives you access to a summary of
the SFTP help. You can call it by typing either of these in the prompt:
help
Available commands:
get remoteFile
/home/demouser/remoteFile
We can copy the remote file to a different name by specifying the name afterwards:
get remoteFile localFile
Transferring Local Files to the Remote System:
put localFile
localFile
For instance, you can change the owner of a file on the remote system with:
Notice how, unlike the system "chmod" command, the SFTP command does not accept usernames, but instead
uses UIDs. Unfortunately, there is no easy way to know the appropriate UID from within the SFTP interface.
get /etc/passwd
!less passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
Notice how instead of giving the "!" command by itself, we've used it as a prefix for a local shell command.
The UID will be in the third column of the file, as delineated by colon characters.
Again, there is no easy way to get a listing of the remote system's groups. We can work around it with the
following command:
get /etc/group
!less group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
The third column holds the ID of the group associated with name in the first column.
FTP
Step 1: Establishing an FTP connection:
ftp domain.com
ftp>
put /path/file
To upload several files we can use the mput command similar to the mget example from above:
mput *.xls
bye
exit
quit
vi editor commands
What is the VI editor?
The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which
make it a widely used editor –
It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
Command mode:
In this mode, you can, move the cursor and cut, copy, paste the text
This mode also saves the changes you have made to the file
Commands are case sensitive. You should use the right letter case
Insert mode:
You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard
Once you are in Insert mode, any key would be taken as an input for the file on which you are currently
working.
To return to the command mode and save the changes you have made you need to press the Esc key
vi <filename_NEW> or <filename_EXISTING>
vi Editing commands:
Note: You should be in the "command mode" to execute these commands. VI editor is case-sensitive so make
sure you type the commands in the right letter-case.
Keystrokes Action
C Delete contents of a line after the cursor and insert new text. Press ESC key
end insertion
r Replace character
S Substitute entire line and begin to insert at the beginning of the line
Make sure you press the right command otherwise you will end up making undesirable changes to the file. You
can also enter the insert mode by pressing a, A, o, as required.
You need to be in the command mode to move within a file. The default keys for navigation are mentioned
below else; You can also use the arrow keys on the keyboard.
Keystroke Use
k Move cursor up
You should be in the command mode to exit the editor and save changes to the file.
Keystroke Use
PING
The ping command is usually used as a simple way to verify that a computer can communicate over the network
with another computer or network device.It is of type ICMP.
Item Explanation
-t Using this option will ping the target until you force it to stop by using
Ctrl-C.
-a This ping command option will resolve, if possible, the hostname of an
IP address target.
-n count This option sets the number of ICMP Echo Requests to send, from 1 to
-f Use this ping command option to prevent ICMP Echo Requests
-i TTL This option sets the Time to Live (TTL) value, the maximum of which
is 255.
In this example, the ping command is used to ping the hostname www.google.com. The -n option tells the ping
command to send 5 ICMP Echo Requests instead of the default of 4, and the -l option sets the packet size for
each request to 1500 bytes instead of the default of 32 bytes.
The result displayed in the Command Prompt window will look something like this:
Ping statistics for 172.217.1.142: Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Item Description
-h MaxHops This tracert option specifies the maximum number of hops in the by
search for the target. If you do not specify MaxHops, and a target has
-w TimeOut You can specify the time, in milliseconds, to allow each reply before
/? Use the help switch with the tracert command to show detailed help
Command:
tracert www.google.com
With the tracert command shown above, we're asking tracert to show us the path from the local computer all the
way to the network device with the hostname www.google.com.
2 35 ms 19 ms 29 ms 98.245.140.1
3 11 ms 27 ms 9 ms te-0-3.dnv.comcast.net [68.85.105.201]
13 81 ms 76 ms 75 ms 209.85.241.37
14 84 ms 91 ms 87 ms 209.85.248.102
15 76 ms 112 ms 76 ms iy-f104.1e100.net [209.85.225.104]
Trace complete.
In this example, we can see that tracert identified fifteen network devices including our router at 10.1.0.1 and all
the way through to the target of www.google.com, which we now know uses the public IP address of
209.85.225.104, one of Google's many IP addresses.
Sed, grep , awk, find,
-Kiran
SED
SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like,
searching, find and replace, insertion or deletion.
SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
SED command in unix supports regular expression which allows it perform complex pattern matching.
Syntax:
Example:
Sample Commands:
1.Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple
sed command replaces the word “unix” with “linux” in the file.
Output :
2.Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second
occurrence of a pattern in a line. The below command replaces the second occurrence of the word “unix” with
“linux” in a line.
Output:
unix is great os. linux is opensource. unix is free os.
3.Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies
the sed command to replace all the occurrences of the string in the line.
Output :
4.Replacing from nth occurrence to all occurrences in a line : Use the combination of /1, /2 etc and /g to
replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the
third, fourth, fifth… “unix” word with “linux” word in a line.
Output:
5.Parenthesize first character of each word : This sed example prints the first character of every word in
paranthesis.
Output:
6.Replacing string on a specific line number : You can restrict the sed command to replace the string on a
specific line number. An example is
Output:
unix is great os. unix is opensource. unix is free os.
The above sed command replaces the string only on the third line.
7.Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on the terminal. If
a line does not have the search pattern and is not replaced, then the /p prints that line only once.
Output:
8.Printing only the replaced lines : Use the -n option along with the /p print flag to display only the replaced
lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only
one time.
Output:
If you use -n alone without /p, then the sed does not print anything.
9.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for
replacing a string.
Here the sed command replaces the lines with range from 1 to 3. Another example is
Output:
Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the
file.
10.Deleting lines from a particular file : SED command can also be used for deleting lines from a particular
file. SED command is used for performing deletion operation without even opening the file
Examples:
Syntax :
Example:
Syntax:
Syntax:
$ sed 'x,yd' filename.txt
Example:
Syntax:
Example:
Syntax:
Example:
grep
grep stands for globally search for regular expression and print out.The grep filter searches a file for a particular
pattern of characters, and displays all lines that contain that pattern.
Syntax:
Options Description
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
Sample Commands:
1. Case insensitive search : The -i option enables to search for a string case insensitively in the give file. It
matches the words like “UNIX”, “Unix”, “unix”.
Output:
2. Displaying the count of number of matches : We can find the number of lines that matches the given
string/pattern
Output:
3. Display the file names that matches the pattern : We can just display the files that contains the given
string/pattern.
$grep -l "unix" *
or
geekfile.txt
4. Checking for the whole words in a file : By default, grep matches the given string/pattern even if it found as a
substring in a file. The -w option to grep makes it match only the whole words.
Output:
5. Displaying only the matched pattern : By default, grep displays the entire line which has the matched string.
We can make the grep to display only the matched string by using the -o option.
Output:
unix
unix
unix
unix
unix
unix
6. Show line number while displaying the output using grep -n : To show the line number of file with the line
matched.
Output:
7. Inverting the pattern match : You can display the lines that are not matched with the specified search sting
pattern using the -v option.
Output:
8. Matching the lines that start with a string : The ^ regular expression pattern specifies the start of a line. This
can be used in grep to match the lines which start with the given string or pattern.
Output:
9. Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line. This can
be used in grep to match the lines which end with the given string or pattern.
11. -f file option Takes patterns from file, one per line:
$cat pattern.txt
Agarwal
Aggarwal
Agrawal
awk
Awk is a scripting language used for manipulating data and generating reports.The awk command programming
language requires no compiling, and allows the user to use variables, numeric functions, string functions, and
logical operators.
1. AWK Operations:
(a) Scans a file line by line
Syntax:
awk options 'selection _criteria {action }' input-file > output-file
Example:
Consider the following text file as the input file for all cases below.
1. Default behavior of Awk : By default Awk prints every line of data from the specified file.
Output:
In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any
argument prints the whole line by default, so it prints all the lines of the file without failure.
In the above example, the awk command prints all the line which matches with the ‘manager’.
For each record i.e line, the awk command splits the record delimited by whitespace character by default and
stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0
represents the whole line.
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example, $1 and $4 represents Name and Salary fields respectively.
Output:
In the above example, the awk command with NR prints all the lines along with the line number.
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example $1 represents Name and $NF represents Salary. We can get the Salary using $NF , where
$NF represents last field.
A B C
Tarun A12 1
Man B6 2
Praveen M42 3
6) To print the first item along with the row number(NR) separated with ” – “ from each line in
geeksforgeeks.txt:
1 - Tarun
2 – Manav
3 - Praveen
A12
B6
M42
$ awk '{ if (length($0) > max) max = length($0) } END { print max }' geeksforgeeks.txt
13
Tarun A12 1
Praveen M42 3
square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25
square of 6 is 36
Output:
find
find command in unix is used to find files and directories and perform subsequent operations on them. It
supports searching by file, folder, name, creation date, modification date, owner and permissions
Syntax :
When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if
you enter ‘Y/y’ it will delete the file.
This command find all empty folders and files in the entered directory or sub-directories.
This command find all the files in the GFG directory or sub-directory with the given
This command print lines which have ‘Geek’ in them and ‘-type f’ specifies the input type is a file.
Gzip, gunzip, tar
-Aashi
File compression commands
Gzip –
- Each single file is compressed into a single file. The compressed file consists of a GNU zip
header and deflated data.
- Compressed file id added with a suffix .gz
- With no arguments gzip compress the standard input and writes the compressed file to standard
output.
Syntax: gzip [options] [filename]
Options:
-f : Sometimes the file cannot be compressed like if a file is there, myfile.gz. As this file already
have .gz extension, it will show error while compressing.
For these scenarios force gzip is used
-r : Compress evey file in a folder and its subfolders. It traverse the directory structure and compress
each file in that folder structure.
-[1-9] : Allow to change the compression level. Default level is 6.
-1/--fast: smaller compression but fast speed.
-9/--best: maximum compression but slow speed.
Gunzip –
Used to compress or expand a file or a list of files in Linux.
Syntax: gunzip [options] [archive name/filename]
Options:
-f : Decopress forcefully.
-V : Version number
tar command in Linux is one of the important command which provides archiving
Syntax:
Options:
-c : Creates Archive
-z : zip, tells tar command that create tar file using gzip
An Archive file is a file that is composed of one or more files along with metadata. Archive
files are used to collect multiple data files together into a single file for easier portability and
Examples:
1. Creating an uncompressed tar Archive using option -cvf : This command creates a tar file
Output :
os2.c
os3.c
os4.c
2. Extracting files from Archive using option -xvf : This command extracts files from
Archives.
Output :
os2.c
os3.c
os4.c
3. gzip compression on the tar Archive, using option -z : This command creates a tar file
5. Creating compressed tar archive file in Linux using option -j : This command compresses
and creates archive file less than the size of the gzip. Both compress and decompress takes
Output :
example.cpp
6. Untar single tar file or specified directory in Linux : This command will Untar a file in
or
7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple
files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract
or
or
or
or
Output :
os1.c
10. list the contents and specify the tarfile using option -tf : This command will list the entire
list of archived file. We can also list for specific content in a tarfile
$ tar tf file.tar
Output :
example.cpp
11. Applying pipe to through ‘grep command’ to find what we are looking for : This command
will list only for the mentioned text or image in grep from archived file.
12. We can pass a file name as an argument to search a tarfile : This command views the
Output :
used to replace or represent one or more characters. Wildcards are typically either an asterisk
(*), which represents one or more characters or question mark (?),which represents a single
character.
Example :
14. To search for an image in .png format : This will extract only files with the extension .png
from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of
the files
to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*)
Note: In above commands ” * ” is used in place of file name to take all the files present in that
particular directory.