Question

In: Computer Science

1. When a file is created on Linux Operating System, What are the default permissions of...

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?

Solutions

Expert Solution

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).


Related Solutions

[Linux permissions] Suppose that you are a superuser on a Linux system, and there is a...
[Linux permissions] Suppose that you are a superuser on a Linux system, and there is a file “/home/alice/foo”, which is owned by an ordinary user Alice. You need to give a permission to read a this file to an ordinary user Bob, but no one else (of course, you as superuser will be able to read it too). Explain how you will do it. Note: You do not have to provide specific commands, just a short description will suffice. [Limitations...
[Linux permissions] Suppose that you are a superuser on a Linux system, and there is a...
[Linux permissions] Suppose that you are a superuser on a Linux system, and there is a file “/home/alice/foo”, which is owned by an ordinary user Alice. You need to give a permission to read a this file to an ordinary user Bob, but no one else (of course, you as superuser will be able to read it too). Explain how you will do it. Note: You do not have to provide specific commands, just a short description will suffice.
Create by using Linux server • Create a file name it foo.txt • Remove all permissions...
Create by using Linux server • Create a file name it foo.txt • Remove all permissions from foo.txt • What happen if you try to read the file? • Change foo.txt permission to read and write only for owner • Change foo.txt permission to read for group • Change foo.txt permission to read and write everyone
In the Linux file system, the majority of the file system code exists in the kernel....
In the Linux file system, the majority of the file system code exists in the kernel. research the anatomy of the Linux file system, then discuss the effectiveness or ineffectiveness of this type of architecture. In addition, explain why you feel the way you do and provide support
application that uses linux operating system amd justify the linux operating system. provide a suitable application...
application that uses linux operating system amd justify the linux operating system. provide a suitable application that uses linux os and justify the usage of linux os in the considered application.
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of mv command The mv command is more than just a wrapper around the rename system call. Write a version of mv that accepts two argument. The first argument must be the name of a file, and the second argument may be the name of a file or the name of a directory. If the destination is the name of a directory, then mv moves...
When working in a Linux operating system, drives such as HDD, Optical and floppy drives are...
When working in a Linux operating system, drives such as HDD, Optical and floppy drives are identified and labeled as? What are similarities and differences between a Linux OS and a Windows OS? Identify how Linux OS's assign serial ports? How are they labeled?
When working in a Linux operating system and you mount a nfs permission on a drive,...
When working in a Linux operating system and you mount a nfs permission on a drive, what default permission are given? Working with swap partition as we have done, identify what a swap partition is and what is it used for? If a system has 16G or RAM, what is normal practice for the swap partition? What is debugfs? what does this command aid in when working on a filesystem?
Study PC operating system such as windows and linux etc and mobile operating system such as...
Study PC operating system such as windows and linux etc and mobile operating system such as Android and iOS, find out whether there are functions and features that are provided in PC OS but not in mobile OS, and if there is any, analyze why these functions and features are not provided by these mobile OS. In your opinion for the future development will PC operating system and mobile operating system be the same or different? Give the details.
Differences Between Linux and Windows Operating Systems Two key differences between a Linux operating system and...
Differences Between Linux and Windows Operating Systems Two key differences between a Linux operating system and a Windows operating system are the concepts of “mounting” and “drive” letters. Provide an example for each. Why is it important to plan disk partitioning before installing Linux? Discuss the advantages of disk partitioning. Also discuss what logical volume management (LVM) is and why or why you might use it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT