面向新用户的 8 个基本 Linux 文件管理命令
我相信基础知识,作为一名前技术讲师,我对 Linux(和其他平台)的新手情有独钟。我写过关于基础知识的文章,我认为是时候介绍一些基本的文件操作命令了。
本文介绍日常任务,例如复制、移动、重命名、创建和删除文件和目录。以下八个命令可使文件管理更加轻松。
那儿有什么?
在管理文件之前,您必须知道有哪些文件。该ls
命令显示当前或指定目录的内容。
当然,ls
有很多有用的选项。以下是我最喜欢的三个:
-l
长格式(显示权限)-a
所有文件,包括隐藏文件-Z
SELinux 上下文
查看目录的内容当然很有用,但查看文件的内容呢?有很多方法可以做到这一点,但我将cat
在这里指出命令。这是一种快速查看文件中内容的方法。
管道符(即|
美式键盘上反斜杠上方的符号)是向终端发出的信号,表示您想要组合命令。它允许您将一个命令的输出“管道化”为另一个命令的输入。
例如,将cat
命令与组合| grep {string}
以在文件内容中搜索特定的字符串。
less
、more
、 most
、head
和等命令tail
都可用于显示文件内容。对于大多数这些命令,语法只是命令名称后跟文件名。
请参阅ls 入门指南 以了解使用该命令的更多技巧ls
。
管理目录
显示文件和目录是一回事,但管理它们又是另一回事。为了更好地组织文件,您可能需要创建一个或多个新目录。您还可能会发现不再需要的目录。
要创建新目录,只需键入命令mkdir
并指定目录名称(如果需要,还要指定目录路径)。这rmdir
将为您删除所有空目录。
不幸的是,在大多数清理场景中,您想要删除的目录都不是空的。在这种情况下,有两个技巧可以提供帮助。第一个是使用一个命令删除目录及其内容,第二个是暂时禁用交互式确认提示。
First, to delete a non-empty directory, use the file-deletion command rm
and add the -R
(recursive) option. To delete the non-empty directory projects
, typerm -R projects
.
Many distributions, RHEL and Fedora among them, prompt users to confirm any deletions. Normally, this is a good thing, but if you're deleting a directory with 100 files in it, you certainly don't want to type y for each file. In that case, use the -f
(force) option. This option overrides the prompt and deletes the specified files. This can be incredibly dangerous, so be very careful and aware before using this option. Make certain you understand what's in the directory you're deleting.
In fact, consider using a trash
command to safeguard yourself from mistakes because, although there's an rm
command, there's no un-rm
, at least not without a great deal of filesystem forensics with a tool like Scalpel or TestDisk.
Manage files
Text editors such as Vim and Nano can create new files, but sometimes it's handy to create an empty file quickly. I used to use the touch
command in classroom demonstrations to make files to work with tools such as tar
, cp
, mv
, and others.
Officially, touch
updates the timestamp on existing files, but one side effect of the command is that if the file doesn't exist, touch
creates it.
To remove a file, use the rm
command. This is the same command used above to delete a non-empty directory. Again, you can use the -f
option to remove files without being prompted for confirmation, but this is dangerous.
Organize files
The cp
command copies files. This is useful for backing up configuration files before making changes to them. The syntax of cp
is simple: copy from here to there. To copy the /etc/ssh/sshd_config
file to your home directory, type cp /etc/sshd_config ~
. The path to the config file is the source, and your home directory is the destination.
Depending on whether the cp
command is aliased to cp -i
on your system, it may overwrite existing files without warning, or it may prompt you to confirm each copy action that may overwrite a file. As with the rm
command, you must take care with regard to overwriting files.
Moving files from one location to another uses the same syntax but with the mv
command. Therefore, moving files is straightforward.
What's more interesting about the mv
command is that it's also the rename command. The logic is that you're moving the file to the same location, just with a different name. So, to rename file1.txt
to fileA.txt
, type mv file1.txt fileA.txt
. Keep in mind that mv
can overwrite existing files, so be sure to check filenames before initiating mv
.
Manage files carefully
Most users who have worked with Linux already know these commands, but with so many new users entering the open source and Linux worlds, a solid and practical review of some basic file-management commands is never bad. Be careful not to accidentally delete or overwrite files with these commands. Take a look at Seth Kenlon's file-management article, too. It includes information on working in a graphical user interface (GUI).