Basic operation mode of Linux file and directory management

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

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

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

  • Absolute path: The path is written from the root directory /, such as: / usr/share/doc.
  • Relative path: The path is not written from /. For example, when / usr/share/doc wants to go under / usr/share/man, it can be written as: CD.. / man. This is the writing method of relative path.

Common commands for working with directories

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

  • ls (English spelling: list files): lists directories and file names
  • cd: change directory
  • pwd (print work directory): displays the current directory
  • mkdir: create a new directory
  • rmdir (English spelling: remove directory): delete an empty directory
  • cp: copy file: copy a file or directory
  • rm: delete a file or directory
  • mv (English spelling: move file): move files and directories, or modify the names of files and directories

You can use man [command] to view the use documents of various commands, such as man cp.

ls (list directory)

In Linux systems, the ls command is probably the most frequently run.

Syntax:

[root@www ~]# Ls [- aadfhilnrst] directory name
[root@www ~]# ls [--color={never,auto,always}] directory name
[root@www ~]# Ls [-- full time] directory name

Options and parameters:

  • -a: All files are listed together with hidden files (files starting with). Common)
  • -d: List only the directory itself, not the file data in the directory (common)
  • -l: Long data list, including file attributes, permissions and other data; (common)

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

[root@www ~]# ls -al ~

cd (switch directory)

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

Syntax:

 cd [Relative path or absolute path]
#Create the runoob directory using the mkdir command
[root@www ~]# mkdir runoob

#Switch to the runoob directory using the absolute path
[root@www ~]# cd /root/runoob/

#Switch to the runoob directory using a relative path
[root@www ~]# cd ./runoob/

# It means to go back to your home directory, that is, the directory / root
[root@www runoob]# cd ~

# It means to go to the current upper level directory, that is, the upper level directory of / root;
[root@www ~]# cd ..

Next, you should have a good understanding of the cd command by operating it several times.

pwd (displays the current directory)

pwd is the abbreviation of Print Working Directory, that is, the command to display the current directory.

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

Options and parameters:

  • -P: Show the exact path instead of using the link path.

Example: simply display the current working directory:

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

The instance shows the actual working directory, not 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 current working directory
[root@www mail]# pwd -P
/var/spool/mail   <==What's going on? Did you add it -P Much worse~
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep  4 17:54 /var/mail -> spool/mail
# See here should know why? Because / var/mail is a link file, link to / var/spool/mail 
# Therefore, with the pwd -P option, the correct full path will be displayed instead of the link file data!

mkdir (create new directory)

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

Syntax:

mkdir [-mp] Directory name

Options and parameters:

  • -m: The permissions of the configuration file! Direct configuration, no need to see the default permission (umask)
  • -p: Help you directly create the required directory (including the upper level directory) recursively!

Example: please try to create several new directories under / tmp:

[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       <== There is no way to create this directory directly!
[root@www tmp]# mkdir -p test1/test2/test3/test4

With this - p option, you can create a multi-layer directory for you!

Instance: create a directory with rwx--x--x permission.

[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 permission section above, if - m is not added to force the configuration of attributes, the system will use the default attributes.

If we use - m, as in the above example, we give - m 711 to give permission to the new directory drwx--x--x.

rmdir (delete empty directory)

Syntax:

 rmdir [-p] Directory name

Options and parameters:

  • -p: From this directory, delete multiple empty directories at one time

Delete runoob directory

[root@www tmp]# rmdir runoob/

Delete the directory (under / tmp) created in the mkdir instance!

[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 deleted directly, no problem
[root@www tmp]# Rmdir test1 < = = cannot delete because there are still contents!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# LS - L < = = look, 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.

Note that this rmdir can only delete empty directories. You can use the rm command to delete non empty directories.

cp (copy file or directory)

cp is copying files and directories.

Syntax:

[root@www ~]# cp [-adfilprsu] source destination
[root@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: To force, if the target file already exists and cannot be opened, remove it and try again;
  • -i: If the destination file already exists, you will first ask about the progress of the action (common) when overwriting
  • -l: Create a hard link link file instead of copying the file itself;
  • -p: Copy it together with the attributes of the file instead of using the default attributes (commonly used for backup);
  • -r: Recursive continuous replication is used for directory replication behavior; (common)
  • -s: Copy it into a symbolic link, that is, a "shortcut" file;
  • -u: If destination is older than source, upgrade destination!

As root, copy. 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 Not covered, y For overlay

Posted by nicob on Mon, 29 Nov 2021 05:25:05 -0800