Name – Ankur Sinhq
Class – D6
Roll No - 56
Experiment No 3
Theory and Screenshots :
Commands for File System Management and User Management
1. ls
Syntax - ls [option] [ le/directory]
Option - -l : Displays all the information about les and directories.
-a : Displays all les including hidden les.
-t : Sorts all les and directories by their last modi cation time.
2. cd
he cd command allows users to change their current working directory within the le sys-
tem.
Syntax - cd [directory]
cd ..
This command is used to move to the root directory.
cd /
This command is used to move to the home directory.
fi
fi
fi
fi
fi
fi
fi
3. pwd
This command is used to print the current working directory.
Syntax – pwd
4. cat
This command is used to print the contents of the le.
5. mkdir
The `mkdir` command is used to create new directories in UNIX and Linux systems.
Syntax - mkdir directory_name
For example - to create a directory named myfolder, you would use:
$mkdir myfolder
To create a subdirectory named morestu in the existing directory named /Documents,
enter: $mkdir /Documents/morestu
ff
ff
fi
To make a subdirectory in a particular directory, you must have permission to write to
that directory.
6. rmdir
The rmdir command is used to remove empty directories in UNIX and Linux systems.
Syntax - $ rmdir [options] directory_name
For example, to remove an empty directory named myfolder, you would use:
$rmdir myfolder
Note: The directory you specify for removal must be empty. To clean it out, switch to the
directory and use the ls and rm commands to inspect and delete les.
7. rm
This command will remove (destroy) a le. You should enter this command with the -i op-
tion, so that you'll be asked to con rm each le deletion.
Syntax - rm [options] le_or_directory_name
Option - -i : To con rm deletion of the le.
-r : To remove a non-empty subdirectory.
-f : This will not prompt you to con rm the removal of each le.
To remove a le named junk, enter: $ rm -i junk
Note: Using rm will remove a le permanently, so be sure you really want to delete a le
before you use rm.
To remove an entire subdirectory named oldstu and all of its contents, enter:
$rm -rf oldstu
Note: Using this command will cause rm to descend into each subdirectory within the
speci ed subdirectory and remove all les without prompting you. Use this command
with caution, as it is very easy to accidentally delete important les. As a precaution, use
the ls command to list the les within the subdirectory you wish to remove.
To browse through a subdirectory named oldstu , enter:
$ ls -R oldstu | less
8. cp
fi
fi
ff
ff
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
ff
ff
fi
fi
fi
fi
The cp command is used to copy les and directories in UNIX and Linux systems.If you
already have a le with the new name, cp will overwrite and destroy the duplicate.
Syntax - $ cp [options] source destination
Option - -i : To force the system to ask for your approval before it destroys any les.
For example - To copy a le named meeting1.txt in the directory /Desktop/notes to your
current directory enter:
$ cp -i /Desktop/notes/meeting1 .
The . (period) indicates the current directory as destination, and the -i ensures that if there
is another le named meeting1 in the current directory, you will not overwrite it by acci-
dent.
To copy a le named old le in the current directory to the new name new le in the
mystu subdirectory of your home directory, enter:
$ cp -i old le ~/mystu /new le
The ~ character (tilde) is interpreted as the path of your home directory.
Note : You must have permission to read a le in order to copy it.
9. mv
This command will move a le. You can use mv not only to change the directory location
of a le, but also to rename les. Unlike the cp command, mv will not preserve the original
le.
Syntax - mv [options] source destination
Option - -i : To prompt before overwriting existing les.
To rename a le named oldname in the current directory to the new name newname, en-
ter:
$ mv -i oldname newname
To move a le named hw1 from a subdirectory named newhw to another sub directory
named oldhw (both subdirectories of the current directory), enter:
$ mv -i newhw/hw1 oldhw
If, in this last operation, you also wanted to give the le a new name, such as rsthw, you
would enter:
$ mv -i newhw/hw1 oldhw/ rsthw
fi
fi
ff
fi
fi
fi
fi
fi
fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
10. chmod
The chmod command is used to change the le mode (permissions) of les and directo-
ries. Like permission to read, write, or execute the le, abbreviated as r, w, and
x. These permissions are broken down for three categories of user
1. Owner of the le;
2. Group with which both the user and the le may be associated
3. All other users.
These categories are abbreviated as u for owner (or user), g for group, and o for other.
Syntax - chmod [options] mode le_or_directory_name
Options -
To allow yourself to execute a le that you own named my le, enter:
$ chmod u+x my le
To allow anyone who has access to the directory in which my le is stored to read or exe-
cute my le, enter:
$ chmod o+rx my le
Note: Be careful with the chmod command. If you tamper with the directory permissions
of your home directory, for example, you could lock yourself out or allow others unre-
stricted access to your account and its contents.
You can view the permission settings of a le using the ls command, described below.
11. wc
The wc command is used to display the number of lines, words, and bytes in les or
standard input in UNIX and Linux systems.
Syntax - wc [options] le_name
Option - -l : Display the number of lines.
-w : Display the number of words.
-c : Display the number of bytes.
-m : Display the number of characters.
-L : Display the length of the longest line.
For example - $ wc wonderland.txt
The rst number is the number of lines, the second is the number of words and the third
is the number of characters in the le.
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
12. Piping and redirection
Let’s say the contents of marks1.txt are as follows,
5 89 92 85
4 98 47 67
1 67 82 76
2 78 97 60
3 67 68 69
The solution would be as below :
$ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - or
$ cut -d " " -f 2- marks1.txt > /tmp/m_tmp.txt
$ paste -d " " students.txt m_tmp.txt
Let’s rst try to understand the second solution, which is a two step solution. Later, we
shall look at the rst solution.
The standard output (stdout), in general, streams (or goes) to the display. Hence, the out-
put of the commands that we type, come out to the display. This may not always be what
we require.
For instance, in the solution above, we use the cut command and get only the required
columns of the le and write the output to a new temporary le. The > character is used
to state that we wish to redirect the output, and it is followed by the location to which we
wish to redirect.
$ command > le1
In general, this creates a new le at the speci ed location, to which the output is written.
But, if we wish to append the output to an existing le, we use >>.
Similarly, the standard input (stdin) is assumed to be from the keyboard. Instead we could
redirect the input from a le.
$ command < le1
The input and the output redirection could be combined in a single command.
$ command < in le > out le
There is actually a third kind of standard stream, called the Standard error (stderr). Any
error messages that you get, are coming through this stream. Like stdout, stderr also
streams to the display, by default but it could be redirected to a le, as well.
For instance, let’s introduce an error into the cut command used before. We change the -f
option to -c
$ cut -d " " -c 2- marks1.txt > /tmp/m_tmp.txt
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
This prints an error that says the delimiter option should be used with the elds option
only, and you can verify that the m_tmp.txt le is empty. We can now, redirect the stderr
also to a le, instead of showing it on the display.
$ cut -d " " -f 2- marks1.txt 1> /tmp/m_tmp.txt 2> /tmp/m_err.txt
The above command redirects all the errors to the m_err.txt le and the output to the
m_tmp.txt le. When redirecting, 1 stands for stdout and 2 stands for stderr. That brings
us to the end of the discussion on redirecting.
The second command in the solution of the problem is trivial to understand.
$ paste -d " " students.txt m_tmp.txt
So, in two steps we solved the problem of getting rid of the roll numbers from the marks
le and displaying the marks along with the names of the students. Now, that we know
how to redirect output, we could choose to write the output to a le, instead of showing
on the display.
Piping
Let us now look at the rst solution.
$ cut -d " " -f 2- marks1.txt | paste -d " " students.txt -
First of all, the hyphen at the end is to ask the paste command to read the standard input,
instead of looking for a FILE. The man page of paste command gives us this information.
Now, what is happening with the cut command. It is a normal cut command, if we looked
at the command only up to the | character. So, the | seems to be joining the commands in
some way.
Essentially, what we are doing is, to redirect the output of the rst command to the
stdin and the second command takes input from the stdin. More
generally,
$ command1 | command2
executes command1 and sends it’s output to the stdin, which is then used as the input
for the command2. This activity is commonly called piping, and the character | is called a
pipe.
fi
fi
fi
fi
fi
fi
fi
fi
fi
This is roughly equivalent to using two redirects and a temporary le
$ command1 > temp le
$ command2 < temp le
$ rm temp le
Also, given that a pipe is just a way to send the output of the command to the stdin, it
should be obvious, to you that we can use a chain of pipes. Any number of commands
can be piped together and you need not be restricted to two commands.
Using piping and redirection, we can do a whole bunch of complex tasks combined with
the commands we have already looked at, and other commands that we are going to look
at.
13. grep
The grep command is used to search for speci c patterns within les or input provided
through standard input. It stands for "global regular expression print" and is a powerful
tool for searching and ltering text.
Syntax - grep pattern [ le…]
To nd word innings in le named abc.txt enter:
$grep innings abc.txt
The sort command is used to sort lines of text les or input.
tr, echo, sort, head, tail, di , comm, less, more, le, type, wc, split, cmp, tar, nd, vim,
gzip, bzip2, unzip, locate,
fi
fi
fi
fi
fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
References-
https://kb.iu.edu/d/afsk
https://fossee.in/sdes-iitb/ult-session.html