English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux File and Directory Management

We know that the directory structure of Linux is a tree structure, with the top-level directory being the root directory /.

Other directories can be added to the tree through mounting, and they can be removed by unmounting.

Before starting this tutorial, we need to know what absolute path and relative path are.

  • Absolute path:
    The format of the path is from the root directory / Start, for example: /usr/share/doc This directory.

  • Relative path:
    The format of the path is not by / Start, for example, by /usr/share/doc to /usr/share/When using man, it can be written as: cd ../man This is the format for writing relative paths.

Common commands for handling directories

Next, let's look at some common commands for handling directories:

  • ls (full name: list files): List directory and filenames

  • cd (full name: change directory): Change directory

  • pwd (full name: print work directory): Display the current directory

  • mkdir (full name: make directory): Create a new directory

  • rmdir (full name: remove directory): Delete an empty directory

  • cp (full name: copy file): Copy files or directories

  • rm (full name: remove): Delete files or directories

  • mv (full name: move file): Move files and directories, or change the name of files and directories

You can use man [command] To view the usage documentation of various commands, such as: man cp.

ls (List Directory)

In the Linux system, the ls command may be the most frequently executed.

Syntax:

[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  :List all files, including hidden files (files starting with .) (commonly used)

  • -d  :Only list the directory itself, not the file data inside the directory (commonly used)

  • -l  :Long data string listing, including file attributes and permissions, etc. (commonly used)

List all files in the home directory (including attributes and hidden files)

[root@www ~]# ls -al ~

cd (Change Directory)

cd is the abbreviation for Change Directory, which is used to change the working directory.

Syntax:

 cd [relative path or absolute path]
#Use the mkdir command to create w3codebox directory
[root@www ~]# mkdir w3codebox
#Use absolute path to switch to w3codebox directory
[root@www ~]# cd /root/w3codebox/
#Use relative path to switch to w3codebox directory
[root@www ~]# cd ./w3codebox/
# Means going back to your home directory, that is /The directory root
[root@www w3codebox]# cd ~
# Means going to the upper-level directory of the current one, that is /The upper-level directory of root;
[root@www ~]# cd ..

With more operations, everyone should be able to understand the cd command well.

pwd (Show the current directory)

pwd is Print Working Directory The abbreviation, which is the command to show the current directory.

[root@www ~]# pwd [-P]

Options and Parameters:

  • -P :Show the actual path, not the path using the link (link) path.

Example: Simply show the current working directory:

[root@www ~]# pwd
/root   <== Show the directory!

The example shows the actual working directory, not just the directory name of the link file itself.

[root@www ~]# cd /var/mail   <==Note that,/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's going on? Did you add -P The difference is quite a lot~
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep  4 17:54 /var/mail -> spool/mail
# You should know why by now? Because /var/mail is a link file, linked to /var/spool/mail 
# So, add pwd -P option, it will not display the data of the link file, but show the correct complete path!

mkdir (create new directory)

If you want to create a new directory, then use mkdir (make directory)!

Syntax:

mkdir [-mp] Directory name

Options and Parameters:

  • -m : configure the file permissions! Configure directly, no need to see the face of the default permissions (umask)!

  • -p : helps you directly create the required directory (including the parent directory) recursively!

Example: Please go to/Try to create several new directories under tmp to see:

[root@www ~]# cd /tmp
[root@www tmp]# mkdir test        <==Create a new directory named test
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4': 
No such file or directory        <==Unable to create this directory directly!
[root@www tmp]# mkdir -p test1/test2/test3/test4

With this -p option, you can help you create multi-level directories yourself!

Example: Create permissions as rwx--x--x directory.

[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

The permission part above, if not added -m to force the configuration properties, the system will use 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)

Syntax:

 rmdir [-p] Directory name

Options and Parameters:

  • -p :Also delete the 'empty' parent directory

Delete w3codebox directory

[root@www tmp]# rmdir w3codebox/

Delete the directory created in the mkdir example:/tmp below) delete it!

[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        <==It can be directly deleted, no problem
[root@www tmp]# rmdir test1  !<==Due to remaining content, it cannot be deleted!
rmdir: `test'1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l <== Look, in the output below, test and test1disappeared!
drwx--x--x  2 root root 4096 Jul 18 12:54 test2

Using -p this option, you can immediately copy test1/test2/test3/test4 Delete once.

However, it should be noted that this rmdir can only delete empty directories, and you can use the rm command to delete non-empty directories.

cp (copy file or directory)

cp is used to copy files and directories.

Syntax:

[root@www ~]# cp [-adfilprsu]: source file (source) target file (destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory

Options and Parameters:

  • -a:Is equivalent to -Means pdr, for pdr, please refer to the following description; (commonly used)

  • -d:If the source file is a link file, it will copy the link file attributes instead of the file itself;

  • -f:Means force, if the target file already exists and cannot be opened, it will remove it and try again;

  • -i:If the target file (destination) already exists, it will ask for action before overwriting (commonly used)

  • -l:Create a hard link (hard link) instead of copying the file itself;

  • -p:Copy the file attributes along with the file, instead of using default attributes (commonly used for backup);

  • -r:Recursive copy continuously, used for directory copying operations; (commonly used)

  • -s:Copy as a symbolic link (symbolic link), that is, a shortcut file;

  • -u:Only upgrade destination if destination is older than source!

Using root privileges, copy the .bashrc under the root directory to /tmp under, and named bashrc

[root@www ~]# cp ~/.bashrc /tmp/bashrc
[root@www ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite `/tmp/bashrc'? n <== n do not overwrite, y to overwrite

rm (remove file or directory)

Syntax:

 rm: [-fir]: file or directory

Options and Parameters:

  • -f: means force, ignores non-existent files without displaying warning messages;

  • -i: interactive mode, it will ask the user whether to proceed before deletion

  • -r  :Recursive deletion! It is most commonly used for deleting directories! This is a very dangerous option!!!

Delete the bashrc created in the cp example!

[root@www tmp]# rm -i bashrc
rm: remove regular file `bashrc'? y

If you add -The i option will actively ask to avoid deleting the wrong file name!

mv (move files and directories, or rename)

Syntax:

[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory

Options and Parameters:

  • -f  :Force, if the target file already exists, it will not ask and will overwrite directly;

  • -i  :If the target file (destination) already exists, it will ask whether to overwrite!

  • -u  :If the target file already exists and the source is newer, it will be upgraded (update)

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

To move a file to a directory, it is done like this!

Rename the directory name just created to mvtest2

[root@www tmp]# mv mvtest mvtest2

Linux File Content View

In the Linux system, use the following commands to view the content of files:

  • cat   Starts displaying the file content from the first line

  • tac   Starts from the last line and shows, which shows that tac is the reverse of cat!

  • nl   When displaying, also output line numbers!

  • more displays the content of the file page by page

  • less is similar to more, but better than more is that it can also go back to the previous page!

  • head to view only the first few lines

  • tail to view only the last few lines

You can use man [command]To view the usage documentation of various commands, such as: man cp.

cat

Start displaying the file content from the first line

Syntax:

cat [-AbEnTv]

Options and Parameters:

  • -A  :Equivalent to -vET's integration options, which can list some special characters instead of just blank spaces;

  • -b  :List line numbers, line numbers are displayed only for non-empty lines, and no line numbers are displayed for blank lines!

  • -E : display the end-of-line byte $

  • -n : print line numbers, including blank lines, and is similar to -b's options are different;

  • -T : display the [tab] key as ^I

  • -v : list some invisible special characters

View /etc/The content of the issue file:

[root@www ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m

tac

tac is the opposite of the cat command, the file content starts from the last line, so tac is the reverse of cat! For example:

[root@www ~]# tac /etc/issue
Kernel \r on an \m
CentOS release 6.4 (Final)

nl

Display line numbers

Syntax:

nl [-bnw] file

Options and Parameters:

  • -b : specifies the way of line number display, mainly two types:
     -b a : represents that line numbers are displayed regardless of whether they are blank lines (similar to cat -n);
     -b t : if there are blank lines, do not display line numbers for those blank lines (default value);

  • -n : there are mainly three ways to display line numbers:
     -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 column without a prefix 0;
     -n rz : the line number is displayed at the far right of the column and is prefixed with 0;

  • -w : the number of digits occupied by the line number column.

Example 1: Using nl to list /etc/Content of the issue

[root@www ~]# nl /etc/issue
     1  CentOS release 6.4 (Final)
     2  Kernel \r on an \m

more

Page by page scrolling

[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 key point is on this line! Your cursor will also wait for your command here

During the execution of the more program, you have several keys you can press:

  • Space key (space): represents scrolling down 'one page';

  • Enter                                                                       : represents scrolling down 'one line';

  • /String                                                                     : represents searching for the keyword 'string' in the displayed content below;

  • :f                                                                           : immediately displays the file name and the current line number;

  • q                                                                               : represents immediately leaving more and no longer displaying the file content.

  • b or [ctrl]-b : represents scrolling back a page, but this action is only useful for files, not for pipelines.

less

Page by page scrolling, the following is an example output/etc/man.config file content:

[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)....
:  <== Here you can enter commands!

The commands that can be entered while less is running are:

  • Space bar : Downward scroll one page;

  • [pagedown] : Downward scroll one page;

  • [pageup] : Upward scroll one page;

  • /string : Downward search for the 'string' function;

  • ?string : Upward search for the 'string' function;

  • n : Repeat the previous search (with / or ? for information!)

  • N : Reverse to repeat the previous search (with / or ? for information!)

  • q : Leave the less program

head

Extract the first few lines of the file

Syntax:

head [-n [number] file

Options and Parameters:

  • -n : Followed by a number, indicating how many lines to display

[root@www ~]# head /etc/man.config

By default, the first 10 lines! To display the first 20 lines, it should be like this:

[root@www ~]# head -n 20 /etc/man.config

tail

Extract the last few lines of the file

Syntax:

tail [-n [number] file

Options and Parameters:

  • -n : Followed by a number, indicating how many lines to display

  • -f : Represents the file name to be monitored continuously, and it needs to be pressed [ctrl]-c to end the tail detection

[root@www ~]# tail /etc/man.config
# By default, the last ten lines are displayed! To display the last 20 lines, it should be like this:
[root@www ~]# tail -n 20 /etc/man.config