31 How do you know what your default shell is 32 If there is

31) How do you know what your default shell is?

32) If there is a long command: ls –l | grep Mon | wc –l

How will you use a shorter command to replace this long command to

reach same purpose?

33) If the path for bash is: /bin/bash, then what you need to type in for the first line in your bash script?

34) How do you use 2 commands to create the following hierarchical tree?

(directories: d2, new, dir2, dir3, dir4, dir5. Files: g3, g4 under dir4, g2, g7 under dir2)

Solution

31) How do you know what your default shell is?

ans:

I read that terminal is nothing but shell. Then unix provide different flavors of shells:

You can type the following command in your terminal to see which shell you are using:

current shell instance, look for the process (shell) having the PID of the current shell instance.

To find the PID of the current instance of shell:

Now to find the process having the PID:

Putting it together:

32) If there is a long command: ls –l | grep Mon | wc –l

How will you use a shorter command to replace this long command to

reach same purpose?

ans:

1) grep pattern files — search for pattern in files

2) grep -r pattern dir — search recursively for pattern in dir

3) grep -rn pattern dir — search recursively for pattern in dir and show the line number found

4)grep -r pattern dir --include=\'*.ext — search recursively for pattern in dir and only search in files with .ext extension

5)command | grep pattern — search for pattern in the output of command

find file — find all instances of file in real system

locate file — find all instances of file using indexed database built from the updatedb command. Much faster than find

sed -i \'s/day/night/g\' file — find all occurrences of day in a file and replace them with night - s means substitude and g means global - sed also supports regular expressions

33) If the path for bash is: /bin/bash, then what you need to type in for the first line in your bash script?

ans:

Running a Bash script is fairly easy. Another term you may come across is executing the script (which means the same thing). Before we can execute a script it must have the execute permission set (for safety reasons this persmission is generally not set by default). If you forget to grant this permission before running the script you\'ll just get an error message telling you as such and no harm will be done.

user@bash: ./myscript.sh

bash: ./myscript.sh: Permission denied

user@bash: ls -l myscript.sh

-rw-r--r-- 18 ryan users 4096 Feb 17 09:12 myscript.sh

user@bash: chmod 755 myscript.sh

user@bash: ls -l myscript.sh

-rwxr-xr-x 18 ryan users 4096 Feb 17 09:12 myscript.sh

user@bash: ./myscript.sh

Hello World!

user@bash

34) How do you use 2 commands to create the following hierarchical tree?

(directories: d2, new, dir2, dir3, dir4, dir5. Files: g3, g4 under dir4, g2, g7 under dir2)

ans:

It is a simple matter to create a hierarchy of directories, also called a directory tree, with a single command in a Unix-like operating system. This can be more convenient and provide greater consistency of the directory structure than issuing a series of separate commands or creating the structure on an ad hoc basis.

Creation of an entire directory tree can be accomplished with the mkdir command, which (as its name suggests) is used to make directories. The -p option tells mkdir to create not only a subdirectory but also any of its parent directories that do not already exist.

For example, to create the hierarchy of directories 2006/seattle/plans/marketing beginning in the current directory (i.e., the directory in which the user is currently working), all that is necessary is to issue the following command:

mkdir -p 2006/seattle/plans/marketing

This single command takes the place of a minimum of four and as many as seven separate commands, i.e., creating four separate directories with mkdir and changing into the first three of them with the cd (i.e., change directory) command.

Branches can easily be added without having to change the current directory. For example, a branch portland/plans/marketing that begins in the directory 2006 could be added with the following:

mkdir -p 2006/portland/plans/marketing

A multi-branched directory tree can be created with a single command rather than with a separate command for each branch. This is accomplished by following mkdir -p with the path (i.e., sequence of directories) of each branch beginning with the current directory. Thus, for example, the tree beginning with 2006 and having the two branches seattle and portland can be created as follows:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing

Any number of subdirectories or branches can be created simultaneously by including them in the same command. For example, the following would additionally create a branch called sales/results that begins in the seattle subdirectory:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing 2006/seattle/sales/results

Each branch is separated by a single space, and the entire command is placed on a single line, although on narrow screens it might appear to be on multiple lines.

If using a GUI (graphical user interface), it can be convenient, particularly in the case of larger or more complex trees, to first write (and edit as needed) the command with a text editor, such as gedit or kedit, and then use the copy and past functions to transfer it to the terminal window (i.e., an all-text mode window in a GUI).

The contents of the new hierarchy of directories can be viewed with a single command by using the ls command with its -R (i.e., recursive) option. For the above example this can be accomplished with the following:

ls -R 2006

A disadvantage of this command is that the results can be spread out over numerous lines and not easy to read. A more concise view can be obtained with the du (i.e., disk usage) command, which is commonly used to show the sizes of directories. For example, du could be used with the directory tree created in the above example as follows:

du 2006

The du command has the additional advantage of showing the size of each directory, which can be useful if files are added to the directories. The default unit is kilobytes.

If an error has been made in creating the hierarchy of directories, it is easy to remove the entire hierarchy by using the rm command with its -r option. For example, the following would delete the entire 2006 directory tree, including any files in it:

rm -r 2006

It is a simple matter to create a hierarchy of directories, also called a directory tree, with a single command in a Unix-like operating system. This can be more convenient and provide greater consistency of the directory structure than issuing a series of separate commands or creating the structure on an ad hoc basis.

Creation of an entire directory tree can be accomplished with the mkdir command, which (as its name suggests) is used to make directories. The -p option tells mkdir to create not only a subdirectory but also any of its parent directories that do not already exist.

For example, to create the hierarchy of directories 2006/seattle/plans/marketing beginning in the current directory (i.e., the directory in which the user is currently working), all that is necessary is to issue the following command:

mkdir -p 2006/seattle/plans/marketing

This single command takes the place of a minimum of four and as many as seven separate commands, i.e., creating four separate directories with mkdir and changing into the first three of them with the cd (i.e., change directory) command.

Branches can easily be added without having to change the current directory. For example, a branch portland/plans/marketing that begins in the directory 2006 could be added with the following:

mkdir -p 2006/portland/plans/marketing

A multi-branched directory tree can be created with a single command rather than with a separate command for each branch. This is accomplished by following mkdir -p with the path (i.e., sequence of directories) of each branch beginning with the current directory. Thus, for example, the tree beginning with 2006 and having the two branches seattle and portland can be created as follows:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing

Any number of subdirectories or branches can be created simultaneously by including them in the same command. For example, the following would additionally create a branch called sales/results that begins in the seattle subdirectory:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing 2006/seattle/sales/results

Each branch is separated by a single space, and the entire command is placed on a single line, although on narrow screens it might appear to be on multiple lines.

If using a GUI (graphical user interface), it can be convenient, particularly in the case of larger or more complex trees, to first write (and edit as needed) the command with a text editor, such as gedit or kedit, and then use the copy and past functions to transfer it to the terminal window (i.e., an all-text mode window in a GUI).

The contents of the new hierarchy of directories can be viewed with a single command by using the ls command with its -R (i.e., recursive) option. For the above example this can be accomplished with the following:

ls -R 2006

A disadvantage of this command is that the results can be spread out over numerous lines and not easy to read. A more concise view can be obtained with the du (i.e., disk usage) command, which is commonly used to show the sizes of directories. For example, du could be used with the directory tree created in the above example as follows:

du 2006

The du command has the additional advantage of showing the size of each directory, which can be useful if files are added to the directories. The default unit is kilobytes.

If an error has been made in creating the hierarchy of directories, it is easy to remove the entire hierarchy by using the rm command with its -r option. For example, the following would delete the entire 2006 directory tree, including any files in it:

rm -r 2006

rm -r is a very powerful -- and dangerous -- command, and thus it should be used with caution.
31) How do you know what your default shell is? 32) If there is a long command: ls –l | grep Mon | wc –l How will you use a shorter command to replace this long
31) How do you know what your default shell is? 32) If there is a long command: ls –l | grep Mon | wc –l How will you use a shorter command to replace this long
31) How do you know what your default shell is? 32) If there is a long command: ls –l | grep Mon | wc –l How will you use a shorter command to replace this long
31) How do you know what your default shell is? 32) If there is a long command: ls –l | grep Mon | wc –l How will you use a shorter command to replace this long

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site