In: Computer Science
1. When a file is created on Linux Operating System, What are the default permissions of the file?
2. Write a command to find the total disk space used by a specific user on linux system?
3. What is "s" permission bit in a file?
4. Explain the difference between grep and egrep?
5. Write a command to list files where third letter is x or y?
6. Write command to remove array element with id 5?
7. Write a shell script that adds an extension “.deb” to all the files in a directory.
8. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file?
9. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
10. Explain the difference between $* and $@?
11. Given a file, replace all occurrence of word “DEF” with “ABC” from 5th line till end in only those lines that contains word “MNO”
12. Explain the difference between $$ and $!?
13. Write a bash command sequence to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?
14. Write a bash command to find the count of lines containing word “CAT”.
15. What are the 3 standard streams in Linux?
If you have any doubts,please feel free to ask!
1.) The default file permission of a file depends on the umask value.
But usually default file permission in linux is rw_rw_r__ (554 in octal format)
The meaning of this is:-
rw_ for user who created file
rw_ for the group of the user who created the file
r__ for other users
Where r=read,w=write,x=executable.
2.) sudo du -sh /home/user_name
This is the command to get the disk usage of specific user
sudo=super user do
du=disk usage
-s attribute specifies the size of all files inside a directory(here we need a users's home directory's disk usage)
-h attribute is used to show the output in human readable format (in MB,GB etc.)
3.) "s" in permission bit in a file is called sticky bit.
Sticky bit is actually used to indicate whether the file is owned by root or not.
If you run a program with sticky bit 1, then it run with super user permissions.
4.)
grep
grep is a program which scans a specified file or files line by line, returning lines that contain a pattern. A pattern is an expression that specifies a set of strings by interpreting characters as meta-characters. For example the asterisk meta character (*) is interpreted as meaning "zero or more of the preceding element". This enables users to type a short series of characters and meta characters into a grep command to have the computer show us what lines in which files match.
The standard grep command looks like:
grep <flags> '<regular expression>' <filename>
Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don't match), and -l to print the file names of files with lines which match the expression.
egrep
The 'E' in egrep means treat the pattern as a regular expression. "Extended Regular Expressions" is enabled in egrep. egrep (same as grep -E) treats +, ?, |, (, and ) as meta-characters.
In basic grep, the meta-characters ?, +, {, |, (, and ) lose their special meaning.
For example, here grep uses basic regular expressions where the plus is treated literally, any line with a plus in it is returned.
grep "+" myfile.txt
egrep on the other hand treats the "+" as a meta character and returns every line because plus is interpreted as "one or more times".
egrep "+" myfile.txt
Here every line is returned because the + was treated by egrep as a meta character. normal grep would have searched only for lines with a literal +.
5.) ls -a | egrep '^(..(x|y))'
ls -a will list all the files,it is pipelined to the egrep command
egrep '^(..(x|y))' :- Starts (^), then 2 characters(..) then the third character can be x or y (x|y)
6.)
array=("1 2 3 4 5 6 7 8")
# print the old array
echo "Old array"
for i in $array
do
echo $i
done
# delete value is 5
delete=5
array=( "${array[@]/$delete}" )
# print the new array
echo "New array"
for i in $array
do
echo $i
done
7.)
find . -type f -exec mv '{}' '{}'.deb \;
First it finds regular files, them we rename each file with .deb as extension
8.) awk '{ if(NR==10){ print "GHIJKLM"} print $0}' FILENAME > FILENAME
This is a awk command in linux.
We iterate through the file, when we reach line number 10 we print "GHIJKLM".
Also we print each and every line.
Then we redirect the output the same file.
Now the file is chaged.
9.) find . -mtime -5 | wc -l
find . will find files in current directory.
-mtime attribute specifies the number of days.
-5 specifies less than 5 days.
wc -l will return number of lines (Here after pipelining we will get files modified within 5 days)
10.)
$* in bash script returns all the arguments that were passed to the program as a list separated by delimiter(IFS)(by default it is a space).
But the list returned will be a single argument,it can be sent to other commands as only 1 argument.
Whereas $@ will return list of all positional arguments. They can be sent as separate arguments for other commands.
Example:
Extras:
Positional arguments to a shell script can be accessed as $1,$2 and so on inside the script.
11.)
We can use sed command.
The answer is:
sed -n '5,$p' filename | sed '/MNO/s/DEF/ABC/g'
sed -n '5,$p' will return all the lines starting from 5th line of the file.
We then pipeline it to next command.
sed '/MNO' searches for MNO in the line
'/s/' in sed is used to substitute one string by another.
'/g' in sed means global, do the operation for all the occurrences of the string.
It can also done this way:
cat filename | sed -n '5,$p' | sed '/MNO/s/DEF/ABC/g'
SED command in UNIX/LINUX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion.
12.)
$$ is the process id(PID) of the running script(i.e. the script which you are running itself).
$! contains the process ID of the most recently executed background command
Example:
13.)
cat file1 1> file2 2> file3
cat file1 will read from file1
Output stream 1 will be redirected to file2
Error stream 2 will be redirected to file3
14.) grep -w "CAT" filename | wc -l
grep -w will search for the word
wc -l will print the number of lines returned by grep.
15.) Three standard streams in linux are:
a) Standard Input(stdin).
b) Standard Output(stdout).
c) Standard Error(stderr).