We know that the directory structure of Linux is a tree
structure, and the top directory is the root directory /.
Other directories can be added to the tree by mounting them,
which can be removed by unmounting them.

Before we start this tutorial, we need to know what absolute and
relative paths are.
·
Absolute path:
The way the path is written, written by the root directory / for example: /usr/share/doc.
The way the path is written, written by the root directory / for example: /usr/share/doc.
·
Relative path:
The way the path is written is not written/written. For example, when /usr/share/doc is under /usr/share/man, it can be written as: cd ../man This is how the relative path is written!
The way the path is written is not written/written. For example, when /usr/share/doc is under /usr/share/man, it can be written as: cd ../man This is how the relative path is written!
Common commands for working with directories
Next, let's look at a few common commands for handling
directories:
·
Ls: list directories
·
Cd: switch directory
·
Pwd: display the current directory
·
Mkdir: create a new directory
·
Rmdir: delete an empty directory
·
Cp: copy files or directories
·
Rm: remove files or directories
·
Mv: move files and directories, or modify the names of files and
directories
You can use man
[command] to view the documentation for each command, such as
man cp.
Ls (listing directories)
On Linux systems, the ls command is probably the most commonly
run.
grammar:
[ root@www ~]# ls [- aAdfFhilnrRSt ] directory name [ root@www ~]# ls [-- color ={ never , auto , always }] directory name [ root@www ~]# ls [-- full - Time ] directory name
Options and parameters:
·
-a : All files, along with hidden files (files beginning with .)
are listed together (common)
·
-d : List only the directory itself, not the file data in the
directory (common)
·
-l : long data string list, including file attributes and permissions,
etc.; (common)
List all the files in the home directory (including attributes
and hidden files)
[ root@www ~]# ls - al ~
Cd (switch directory)
Cd is the abbreviation of Change Directory, which is used to
change the working directory.
grammar:
Cd [relative path or absolute path]
#Create the runoob directory using the mkdir command [ root@www ~]# mkdir runoob
#Use absolute path to switch to runoob directory [ root@www ~]# cd / root / runoob /
#Use the relative path to switch to the runoob directory [ root@www ~]# cd ./ runoob /
# indicates return to your home directory, which is /root this directory [ root@www runoob ]# cd ~
# indicates to go to the current upper level directory, which means the upper level directory of /root; [ root@www ~]# cd ..
Next, you should be able to understand the cd command well if
you have to do it a few times.
Pwd (shows the current directory)
Pwd is an abbreviation for Print Working Directory ,
which is a command that displays the current directory.
[ root@www ~]# pwd [- P ]
Options and parameters:
·
-P :
Shows the exact path instead of using the link path.
Example: Simply show the current working directory:
[ root@www ~]# pwd
/ root <== Show the directory~
The example shows the actual working directory, not the
directory name of the link itself.
[ root@www ~]# cd / var / mail <==Note, / var / mail is a link file [ root@www mail ]# pwd
/ var / mail <==list the current working directory [ root@www Mail ]# pwd - P
/ var / spool / mail <== What happened? There is no plus - P is a lot worse ~ [ root@www mail ]# ls - ld / var / mail
Lrwxrwxrwx 1 root root 10 Sep 4 17 : 54 / var / mail -> spool / mail
# See here you should know why? Because /var/mail is a link file, link to /var/spool/mail # So, after adding the pwd -P option, it will not display the data of the link file, but display the correct full path!
Mkdir (create a new directory)
If you want to create a new directory, then use mkdir (make
directory).
grammar:
Mkdir [- mp ] directory name
Options and parameters:
·
-m : Permissions for configuration files! Direct
configuration, do not need to look at the default permissions (umask) face ~
·
-p : Helps you to recursively create the required directories
(including the parent directory)!
Example: Please try to create several new directories under /tmp
to see:
[ root@www ~]# cd / tmp
[ root@www tmp ]# mkdir test <==Create a new directory for test [ root@www tmp ]# mkdir test1 / test2 / test3 / test4
Mkdir : cannot create directory `test1/test2/test3/test4':
No such file or directory <== There is no way to create this directory directly!
[root@www tmp]# mkdir -p test1/test2/test3/test4
Add this -p option to create a multi-level directory for you!
Example: Create a directory with permissions rwx--x--x .
[ root@www tmp ]# mkdir - m 711 test2
[ root@www tmp ]# ls - l
Drwxr - xr - x 3 root root 4096 Jul 18 12 : 50 test
Drwxr - xr - x 3 root root 4096 Jul 18 12 : 53 test1
Drwx -- x -- x 2 root root 4096 Jul 18 12 : 54 test2
In the permissions section above, if you do not apply -m to
force configuration properties, the system uses the default properties.
If we use -m , as in the example above we give -m 711 to give
the new directory drwx--x--x permission.
Rmdir (delete empty directory)
grammar:
Rmdir [- p ] directory name
Options and parameters:
·
-p : Also
deleted along with the previous "empty" directory
Delete the runoob directory
[ root@www tmp ]# rmdir runoob /
Delete the directory created under the mkdir instance (under the
/tmp)!
[ root@www tmp ]# ls - l <==See how many directories exist?
Drwxr - xr - x 3 root root 4096 Jul 18 12 : 50 test
Drwxr - xr - x 3 root root 4096 Jul 18 12 : 53 test1
Drwx -- x -- x 2 root root 4096 Jul 18 12 : 54 test2
[ root@www tmp ]# rmdir test <== can be deleted directly, no problem [ root@www tmp ]# rmdir test1 <== because There is content, so I can't delete it!
Rmdir : `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l <==Look, the test and test1 are missing in the output below!
Drwx--x--x 2 root root 4096 Jul 18 12:54 test2
With the -p option, test1/test2/test3/test4 can be deleted at
once.
However, it should be noted that this rmdir can only delete
empty directories. You can use the rm command to delete non-empty directories.
Cp (copy files or directories)
Cp is a copy of files and directories.
grammar:
[ Root @ the WWW ~] # cp [- adfilprsu ] Source files ( Source ) target file ( Where do you want ) [ root @ the WWW ~] # cp [ Options ] source1 source2 Source3 .... Directory
Options and parameters:
·
-a: Equivalent
to -pdr. For pdr, please refer to the following instructions; (common)
·
-d: If
the source file is a link file attribute, copy the link file attribute instead
of the file itself;
·
-f: means
force. If the target file already exists and cannot be opened, try again after
removing it.
·
-i: If
the destination already exists, the action will be asked first when overwriting
(common)
·
-l: Create
a hard link link file instead of copying the file itself;
·
-p: Copy
the past along with the attributes of the file instead of using the default
attributes (backup commonly used);
·
-r: recursive
continuous replication, used for directory replication behavior; (common)
·
-s: copy
into a symbolic link (symbolic link), which is a "shortcut" file;
·
-u: If
the destination is older than the source, upgrade the destination!
As root, copy the .bashrc in the root directory to /tmp and name
it bashrc
[ root@www ~]# cp ~ /.bashrc / tmp / bashrc
[ root@www ~]# cp - i ~ /.bashrc / tmp / bashrc
Cp : overwrite `/tmp/bashrc'? n <==n does not cover, y is overwritten
Rm (remove files or directories)
grammar:
Rm [- fir ] file or directory
Options and parameters:
·
-f : is the meaning of force, ignore files that do not exist, no
warning message will appear;
·
-i : interactive mode, asks the user whether to action before
deleting
·
-r : Recursively delete! Most commonly used in the deletion
of the directory! This is a very dangerous option! ! !
Remove the bashrc you just created in the instance of cp!
[ root@www tmp ]# rm - i bashrc
Rm : remove regular file `bashrc'? y
If you add the -i option, you will be asked to avoid the wrong
file name!
Mv (move files and directories, or modify the name)
grammar:
[ root@www ~]# mv [- fiu ] source destination
[ root@www ~]# mv [ options ] source1 source2 source3 .... directory
Options and parameters:
·
-f :force Forces the meaning, if the target file already exists,
it will be overwritten without asking;
·
-i : If the destination file already exists, it will ask if it
is overwritten!
·
-u : will be upgraded if the target file already exists and the
source is newer.
Copy a file, create a directory, and move the file to the
directory
[ root@www ~]# cd / tmp
[ root@www tmp ]# cp ~/. bashrc bashrc
[ root@www tmp ]# mkdir mvtest
[ root@www tmp ]# mv bashrc mvtest
Move a file to a directory and that's it!
Rename the directory name just to mvtest2
[ root@www tmp ]# mv mvtest mvtest2
Linux file content view
Use the following command on Linux to view the contents of the
file:
·
Cat displays the file content starting from the first line
·
Tac starts from the last line, you can see that tac is the
reverse write of cat!
·
When nl is displayed, the line number is output!
·
More page display content
·
Less is similar to more, but better than more, he can turn pages
forward!
·
Head, only the first few lines
·
Tail only a few lines of the tail
You can use man
[command] to view the documentation for each command, such as
man cp.
Cat
Display file content starting from the first line
grammar:
Cat [- AbEnTv ]
Options and parameters:
·
-A : Equivalent to the integration option of -vET, which can
list some special characters instead of blanks;
·
-b : List line numbers, only line numbers are displayed for
non-blank lines, blank lines are not marked with line numbers!
·
-E : Display the trailing line byte $ at the end;
·
-n : prints the line number, along with the blank line, there
will also be a line number, which is different from the -b option;
·
-T : Display the [tab] button as ^I;
·
-v : list some special characters that cannot be seen
Check the contents of the /etc/issue file:
[ root@www ~]# cat / etc / issue
CentOS release 6.4 ( Final ) Kernel \r on an \m
Tac
The tac and cat commands are just the opposite. The contents of
the file are displayed from the last line. It can be seen that tac is the
reverse write of cat! Such as:
[ root@www ~]# tac / etc / issue
Kernel \r on an \m
CentOS release 6.4 ( Final )
Nl
Display line number
grammar:
NL [- BNW ] file
Options and parameters:
·
-b : Specify the way the line number is specified. There are two
main ways:
-ba : indicates that the line number is similar (cat-n) regardless of whether it is a blank line or not;
-bt : if there is a blank line, the empty line Do not list the line number (default);
-ba : indicates that the line number is similar (cat-n) regardless of whether it is a blank line or not;
-bt : if there is a blank line, the empty line Do not list the line number (default);
·
-n : Lists the methods represented by the line number. There are
three main methods:
-n ln : the line number is displayed at the far left of the screen;
-n rn : the line number is displayed at the far right of the field, without adding 0 ;
-n rz : The line number is displayed at the far right of its field, and is incremented by 0;
-n ln : the line number is displayed at the far left of the screen;
-n rn : the line number is displayed at the far right of the field, without adding 0 ;
-n rz : The line number is displayed at the far right of its field, and is incremented by 0;
·
-w : The number of bits occupied by the line number field.
Example 1: Listing the contents of /etc/issue with nl
[ root@www ~]# nl / etc / issue
1 CentOS release 6.4 ( Final ) 2 Kernel \r on an \m
More
Page by page
[ root@www ~]# more / etc / man_db . config
# # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d .... (omitted in the middle).. .. -- More --( 28 %) <==The focus is on this line! Your cursor will also wait for your order here.
During the running of this program, you have several buttons to
press:
·
Blank key: represents a page down;
·
Enter : Represents scrolling down "one line";
·
/String: Represents the keyword "string" in the
content of this display;
·
:f : Immediately display the file name and the number of lines
currently displayed;
·
q : The delegate immediately leaves more and no longer displays
the contents of the file.
·
b or [ctrl]-b : Represents turning back pages, but this action
is only useful for files and not useful for pipelines.
Less
Page flipping, the following example outputs the contents of the
/etc/man.config file:
[ root@www ~]# less / etc / man . config
# # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d .... (omitted in the middle).. .. : <== You can wait for your input command here!
The commands that can be entered while running are:
·
Blank key: Scroll down one page;
·
[pagedown]: Scroll down one page;
·
[pageup] : Flip up one page;
·
/string: Search for the function of "string" down;
·
? String: Search for the function of "string" upwards;
·
n : Repeat the previous search (related to / or ?)
·
N : Reverse the previous search (related to / or ?)
·
q : leave the program less;
Head
Remove the first few lines of the file
grammar:
Head [- n number ] file
Options and parameters:
·
-n : followed by a number, which means the meaning of displaying
a few lines
[ root@www ~]# head / etc / man . config
By default, the first 10 lines are displayed! To display
the first 20 lines, you have to do this:
[ root@www ~]# head - n 20 / etc / man . config
Tail
Take the last few lines of the file
grammar:
Tail [- n number ] file
Options and parameters:
·
-n : followed by a number, which means the meaning of displaying
a few lines
·
-f : Indicates that the file name is connected after continuous
detection. Wait until [ctrl]-c is pressed to end the detection of tail.
[ root@www ~]# tail / etc / man . config
# By default, the last ten lines are displayed! To display the last 20 lines, you have to do this: [ root@www ~]# tail - n 20 / etc / man . config
0 Comments