Detailed explanation of common directory file operation commands in Linux

Therefore, if you want to operate Linux proficiently, you can only improve your system operation ability by learning Linux commands one by one.

The first thing we need to learn is the operation command of Linux directory file

cd Change the user's current working directory

cd (options) (arguments)

The cd command is used to switch the user's working directory to a specified directory. You can use an absolute path or a relative path for the specified directory. (The absolute path means starting from the root directory, and the relative path means starting from the current directory)

If you use the cd command directly without any parameters, it will switch to the home directory of the logged in user

For example, if I log in with the root user at the beginning, he will enter the /root/ directory after entering the command line. After using cd to switch to other directories, cd directly (without any parameters), then he will switch back to /root / content

Points to note when cd switches directories

"~" means home directory (home directory or user directory)
 . " . " means the current directory
 . " .. " means the previous directory of the current directory location.
example

cd enters the user's home directory;
cd ~ enters the user's home directory;
cd - returns to the directory where it was before entering this directory;
cd .. returns to the upper-level directory (if the current directory is "/", it will remain in "/" after execution; " .." means the upper-level directory);
cd ../.. returns to the upper-level directory;
cd !$ uses the parameter of the previous command as the cd parameter.

pwd displays the user's current working directory

pwd (options)

pwd is used to display the user's current working path, which is presented in the form of an absolute path

For example the following example

[root@localhost linuxidc]# cd /root
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/linuxidc/
[root@localhost linuxidc]# pwd
/home/linuxidc

ls displays a list of targets (used earlier when talking about directory structure)

ls (options) (arguments)

The parameter can be a directory, if nothing is added, it represents the current directory

The ls directory is, in my opinion, the most frequently used command in Linux.

Under Windows, open the file explorer, enter a directory, and the file directory in it is directly displayed

In Linux, when you enter a directory and want to view the files or folders in a directory, you must use the ls command.

The options of the ls command are as follows

-a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出);
-A:显示除影藏文件“.”和“..”以外的所有文件列表;
-C:多列显示输出结果。这是默认选项;
-l:与“-C”选项功能相反,所有输出信息用单列格式输出,不输出为多列;
-F:在每个输出项后追加文件的类型标识符,具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字。当文件为普通文件时,<br>不输出任何标识符;
-b:将文件中的不可输出的字符以反斜线“”加字符编码的方式输出;
-c:与“-lt”选项连用时,按照文件状态时间排序输出目录内容,排序的依据是文件的索引节点中的ctime字段。与“-l”选项连用时,则排序的一句是文件的状态改变时间;
-d:仅显示目录名,而不显示目录下的内容列表。显示符号链接文件本身,而不显示其所指向的目录列表;
-f:此参数的效果和同时指定“aU”参数相同,并关闭“lst”参数的效果;
-i:显示文件索引节点号(inode)。一个索引节点代表一个文件;
--file-type:与“-F”选项的功能相同,但是不显示“*”;
-k:以KB(千字节)为单位显示文件大小;
-l:以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名,文件类型、权限模式、硬连接数、所有者、组、文件大小和文件的最后修改时间等;
-m:用“,”号区隔每个文件和目录的名称;
-n:以用户识别码和群组识别码替代其名称;
-r:以文件名反序排列并输出目录内容列表;
-s:显示文件和目录的大小,以区块为单位;
-t:用文件和目录的更改时间排序;
-L:如果遇到性质为符号链接的文件或目录,直接列出该链接所指向的原始文件或目录;
-R:递归处理,将指定目录下的所有文件及子目录一并处理;
--full-time:列出完整的日期与时间;
--color[=WHEN]:使用不同的颜色高亮显示不同类型的。

之前跟大家讲过每种颜色分别代表什么样的颜色,在这你也可以通过 --color 自己指定不同类型显示那种不同的颜色

下面是简单的演示

[root@localhost ~]# ls    #默认显示当前目录下的文件
1.txt  2.txt  3.txt  a  b  c
[root@localhost ~]# ls -a    #显示当前目录下所有的文件,包含"."开头的隐藏文件
.  1.txt  3.txt  b              .bash_logout  .bashrc                c      .cshrc    .python_history  .viminfo
..  2.txt  a      .bash_history  .bash_profile  .bashrc-anaconda3.bak  .cache  .ipython  .tcshrc          .vimrc
[root@localhost ~]# ls -l      #显示当前目录下文件的详细信息,如权限,文件大小,修改时间
total 12
-rw-r--r--. 1 root root    0 Apr  5 10:29 1.txt
-rw-r--r--. 1 root root    0 Apr  5 10:29 2.txt
-rw-r--r--. 1 root root    0 Apr  5 10:29 3.txt
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 a
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 b
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 c
[root@localhost ~]# ll       #等同与ls -l 命令,显示文件详细信息
total 12
-rw-r--r--. 1 root root    0 Apr  5 10:29 1.txt
-rw-r--r--. 1 root root    0 Apr  5 10:29 2.txt
-rw-r--r--. 1 root root    0 Apr  5 10:29 3.txt
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 a
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 b
drwxr-xr-x. 2 root root 4096 Apr  5 10:29 c
[root@localhost ~]# ls -al      #组合选项 相当于 ls -a -l 显示当前目录下所有文件或目录的详细信息
total 68
dr-xr-x---.  7 root root 4096 Apr  5 10:29 .
dr-xr-xr-x. 22 root root 4096 Apr  5 09:27 ..
-rw-r--r--.  1 root root    0 Apr  5 10:29 1.txt
-rw-r--r--.  1 root root    0 Apr  5 10:29 2.txt
-rw-r--r--.  1 root root    0 Apr  5 10:29 3.txt
drwxr-xr-x.  2 root root 4096 Apr  5 10:29 a
drwxr-xr-x.  2 root root 4096 Apr  5 10:29 b
-rw-------.  1 root root 4083 Apr  4 18:03 .bash_history
-rw-r--r--.  1 root root  18 May 20  2009 .bash_logout
-rw-r--r--.  1 root root  176 May 20  2009 .bash_profile
-rw-r--r--.  1 root root  247 Mar 12 05:07 .bashrc
-rw-r--r--.  1 root root  176 Mar 11 06:12 .bashrc-anaconda3.bak
drwxr-xr-x.  2 root root 4096 Apr  5 10:29 c
drwxr-xr-x.  3 root root 4096 Apr  1 05:19 .cache
-rw-r--r--.  1 root root  100 Sep 22  2004 .cshrc
drwxr-xr-x.  5 root root 4096 Mar 12 05:10 .ipython
-rw-------.  1 root root  32 Mar 31 17:16 .python_history
-rw-r--r--.  1 root root  129 Dec  3  2004 .tcshrc
-rw-------.  1 root root 4016 Apr  2 04:35 .viminfo
-rw-r--r--.  1 root root  25 Mar 12 05:10 .vimrc

其他选项可以自己动手去试,由于篇幅问题,我就不一一演示了

mv 移动文件目录命令

mv(选项)(参数)

mv命令可以用来移动一个文件或是一个目录,同时也可以用来作为改名的命令

他一般需要两个参数 ,source(源文件或源目录),target(目标文件或目标目录)

注意,如果目标路径中存在相同的文件,那么将会覆盖原先的文件,如果只是移动到当前目录下,而且与原先文件目录名不同,那么这只是一个改名的操作

mv的选项如下

--backup=<备份模式>:若需覆盖文件,则覆盖前先行备份;
-b:当文件存在时,覆盖前,为其创建一个备份;
-f:若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录;
-i:交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。用户输入”y”,表示将覆盖目标文件;输入”n”,表示取消对源文件的移动。这样可<br>以避免误将文件覆盖。
--strip-trailing-slashes:删除源文件中的斜杠“/”;
-S<后缀>:为备份文件指定后缀,而不使用默认的后缀;
--target-directory=<目录>:指定源文件要移动到目标目录;
-u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作。

一般我们再使用mv命令时是用不到选项的,所以只是列举选项以供参考

mv的应用

[root@localhost linuxidc]# ls
a.py  a.sh
[root@localhost linuxidc]# mv a.py b.py      #只是改名了
[root@localhost linuxidc]# ls
a.sh  b.py

[root@localhost linuxidc]# ls /root/a/
1.py  2.py  3.py  4.py  5.py
[root@localhost linuxidc]# pwd
/home/linuxidc
[root@localhost linuxidc]# mv /root/a/* .          #移动a目录下所有文件到当前目录(*代表的是通配符,.代表的是当前目录)
[root@localhost linuxidc]# ls
1.py  2.py  3.py  4.py  5.py  a.sh  b.py

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326479969&siteId=291194637