Assignment
Assignment
1. Can you explain the 'cat' command in Linux and how to use it to concatenate and display files?
Ans. : The cat command reads each File parameter in sequence and writes it to standard output.
If you do not specify a file name, the cat command reads from standard input. You can also
specify a file name of - (dash) for standard input.
The 'cat' command can be used to concatenate the contents of multiple files in a single new file.
Syntax: cat <filename1> <filename2>.... > <newFilename>.
The cat command is the simplest way to view the contents of a file. It displays the contents of
the file(s) specified on to the output terminal.
This will print the contents of the file both.txt:
cat both.txt
This will number each line in the output:
cat -n both.txt
2. What is the 'grep' command in Linux and how do you use it to search for specific patterns in files
and directories?
Ans. : You can specify a pattern to search with either the -e or -f option. If you do not specify
either option, grep (or egrep or fgrep) takes the first non-option argument as the pattern for
which to search. If grep finds a line that matches a pattern, it displays the entire line.
Options
grep accepts all the following options while egrep and fgrep accept all but the -E and -F options.
-A num
Displays num lines of trailing context after the lines are matched.
-B
Disables the automatic conversion of tagged files. This option is ignored if the filecodeset or
pgmcodeset options (-W option) are specified.
-b
Precedes each matched line with its file block number.
-C num
Displays num lines of leading and trailing output context.
-c
Displays only a count of the number of matched lines and not the lines themselves.
-E
Matches using extended regular expressions (causes grep to behave like egrep).
-e pattern
Specifies one or more patterns that are separated by newlines for which grep is to search.
You can indicate each pattern with a separate -e option character, or with newlines within
pattern. For example, the following two commands are equivalent:
3. How do you use the 'sed' command in Linux to replace text in a file or a stream?
Ans. : The Linux sed command is most commonly used for substituting text. It searches for the
specified pattern in a file and replaces it with the wanted string. To replace text using sed , use
the substitute command s and delimiters (in most cases, slashes - / ) for separating text fields.
The procedure to change the text in files under Linux/Unix using sed: Use Stream EDitor (sed) as
follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find
and replace.
The procedure to change the text in files under Linux/Unix using sed:
1. Use Stream EDitor (sed) as follows:
2. sed -i 's/old-text/new-text/g' input.txt
3. The s is the substitute command of sed for find and replace
4. It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named
input.txt
4. Can you explain the difference between 'du' and 'df' commands in Linux and how to use
them to check disk usage?
- du shows the blocks actually allocated to an individual file.
The du command shows the estimated disk space used by files and directories. If you execute
du without any options it will output the sizes of all files starting in your current directory and all
subdirectories of your current directory.
- df shows the blocks allocated in the entire file system, including inodes and other meta data.
df shows the amount of free space that is left on a file system. Space can have the following
values: Space Used. Total amount of space that is allocated to existing files in the file system.
Space Free.
5. How do you use the 'mv' command to move multiple files and directories at once in Linux?
The mv (move) command is used to move one or more files or directories from one directory to
another directory using terminal in the Linux/Unix operating system. After using the mv
command file is copied from source to destination and source file is removed. The mv command
is also used to rename the file.
Syntax :
mv <source_file1> <source_file2> ... <source_directory1> <source_directory2> ...
<destination_directory>
The general syntax of the mv command is as follows
$ mv [OPTION]...[-T] SOURCE DESTINATION...
$ mv [OPTION]...SOURCE... DIRECTORY...
$ mv [OPTION]...-t DIRECTORY SOURCE...
Brief description of options available in the mv command.
1 --backup [=CONTROL]
Create backup of destination file
2 -b
Create backup file without argument
3 -f
Do not prompt before overwriting
4 -i, interactive
Prompt before overwriting files
5 -n, --no-clobber
Not overwrite an existing file
6 -t, --target-directory=DIRECTORY
Move all source arguments into directory
7 -T, --no-target-directory
Treat destination as a normal file
8 -u, --update
Move when source file is newer than the destination file either destination file is missing
9 -v, --verbose
Prompt what is happening
10 --help
Displays a help message and then exits.
11 --version
It gives info about the version and then exits.
6. What command do you use to untar a file in Linux? Also, write a command to create a tar
archive of an already present directory.
This option is used to untar any file in our current directory or inside the specified directory with
the -C option. The command is as follows: $ tar -xvf file. tar -C file's path in the directory.
To untar a file in Linux, you can use the following command:
tar -xvf filename.tar
where filename.tar is the name of the tar archive you want to extract. The -x option tells tar to
extract the archive, the -v option enables verbose output, and the -f option specifies the name
of the tar archive.
To create a tar archive of an already present directory, you can use the following command:
tar -cvf archive.tar directory
where archive.tar is the name of the tar archive you want to create and directory is the name of
the directory you want to archive. The -c option tells tar to create a new archive, and the -v
option enables verbose output.
7. How do you use the 'head' and 'tail' commands in Linux to display the beginning and end of
a file respectively?
The head Command
The syntax of the head command is pretty straightforward:
head [OPTIONS] FILES
$ cat numbers_en.txt
one :1
two :2
three :3
four :4
...
ninety-seven : 97
ninety-eight : 98
ninety-nine : 99
one hundred : 100
The file contains English words of numbers from 1 to 100. Thus, the file has 100 lines.
The head command will, by default, write the first ten lines of the input file to the standard
output:
$ head numbers_en.txt
one :1
two :2
three :3
four :4
five :5
six :6
seven :7
eight :8
nine :9
ten : 10
The syntax of using the tail command is quite straightforward, too:
4. Write a shell script program to demonstrate the usage of while and do-while loop.