05. LINUX commands(4) - Uchiha3000/LINUX_docx GitHub Wiki

LINUX commands part 4

1. Will this command work "mkdir sanjay/kolkata/sha/liner" for creating multiple sub directories?

code: NO, it won't work because the parent directories are not created. For example even if sanjay and kolkata directories are created, the liner directory has no parent directory named sha.

But, if we use it like this "mkdir -p sanjay/kolkata/sha/liner" then it will work because we are creating the parent directory with this code.

2. What is the use of "|" or pie ?

code: ls -l / | cat > AF01

(Note: It is used for combining 2 or more commands. The output of the 1st command will be the input for 2nd command. For example in this code 1st command is "ls -l /" and it's output will be the input for the 2nd command namely "cat > AF01".)

How to input the long list of / or root to a new file, and we want to cont the number of lines in the file. All of this should be done in a single line?

code: ls -l / | cat > AF01 | wc -l

3. Print only all the directories.

code: ls -l | grep drw

(INCORRECT)

(Note: But there is a problem with using a code like this. The problem is if there is a file named drw it will also show that. Basically what we are doing it that first we are printing long list and then using that long list as an input for finding the files or directories that starts with characters of "drw". So the above command is not full proof)

code: ls -lp | grep /

(Note: Now this is a correct and full proof command because first we are printing the long list of files and directories with only the directories having the "/" character, so on the next command we are printing only those name which has a "/".

4.Check how many directories are there?

code: ls -lp | grep / | wc -l

5.How to print only the files?

code: ls -lp | grep -v /

6.How to count the number of files?

code: ls -lp | grep -iv / | grep -v total | wc -l

(Note: We are giving grep -iv total because it excludes the line which shows the total and since we don't know in which case (i.e. Uppercase or lowercase) the total is written so -i which ignores case sensitivity.)

7.How to find a file in LINUX? (This question comes in interview)

code: find /home/ec2-user -type f -name mylist

(Note: find space location-to-be-searched space -type space f space -name space filename. It's f if we want to search for file and d if we want to search for directories. Then to name pf the file to be searched should be written like this "-name mylist". This will show us the file locations)

8.How to find a directory named u03 in LINUX?

code: find /home/ec2-user -type d -name u03

9.How to find a hidden file in LINUX?

(DOUBT)

code: find /home/ec2-user -type f -name .AHidden

10.How to find a directory in LINUX when we don't remember the file location to be searched?

code: find / -type d -name u03

(Note: We are searching from root mount point i.e. /. Amidst all the permission denied you will find the location of your file. All this permission denied is because we are trying to open a file from root mount point)

11.How will you remove all the permission denied by modifying the above code?

doubt: can't search file using this command

code: find / -type d -name u03 2> /home/ec2-user/null02

(Note: Create a file to send the permission denied, for this code we have created null02 then in the last part of the code "2>/home/ec2-user/null02" type this to send the permission denied to null02, "2>"is there because directory sizes are greater than 2 because the default value of directory is 2 and if it has 1 sub directory then 3. Also please note that we can't use the grep command because the permission denied will still be showed)

doubt: in ctrl + shift + c and ctrl + shift + v

(Note: To copy a text in LINUX just select the text and right click on the section you want to pasted. It will be pasted)

In LINUX we use vi editor. In this editor we use different modes

13.How to open a file in vi editor

code: vi aef01

14.How to change the mode of vi editor to edit or insert mode?

code: esc + i

15.How to save and quit the deitor?

code: esc + :wq

(Note: w is for save and q is for quit)

16.How to copy a line in editor?

code: Go to the line and esc + yy

17.How to paste a line in editor?

code: esc + d

18.How to quit without saving?

code: esc + :q!

19.How to just save?

code: esc + :W

20.How to replace a word in editor?

code: Go to the line where you want to replace the word then, esc + :s/test/house (:s/replacing word/replaced word)

21.How to replace all 'test' word in editor?

code: esc + :%s/test/house

22.How to undo a move in editor?

code: esc + U

23.How to create a new line above?

code: Go to the line on which you want to add a line above, then esc + O. (Note: 'O' should be in upper case) Or, go to the end of the line above and press enter.

24.How to create a new line below?

code: Go to the line on which you want to add a line below, then esc + o. (Note: 'o' should be in lower case) Or, go to the end of the line and press enter.

25.How to delete a line?

code: esc + dd

26.How to delete 4 line?

code: esc + 4 + dd

Note: There are three editors in LINUX. They are vi, vim & nano. The most popular and most commonly used is vi.