Basic Operations linux (centOS7) (b) directory and file management

1. Display the absolute path of the current working directory

pwd

2. Display subdirectories and files in the current working directory

ls [-l] [-h] [-a]

If you call only ls, subdirectories and files will simply set out, -l said it would it appear as a detailed list, -h expressed in human-readable format, -a represents at the same time show hidden directories or files, that is dot (.) at the beginning, for example /root/.ssh

3. Change the working directory

cd [path]

Wherein, path is to switch to the target directory, you can use absolute or relative path. ~ path is empty or, a handoff to the current logged-in user's home directory; when the path of two points (..), a handoff to the current directory. The following is an example of application

 

As shown, the current directory is assumed opt, now want to switch to RPM,

Use an absolute path: cd / usr / lib / rpm;

Using a relative path: cd ../../usr/lib/rpm.

4. Create a directory

mkdir [-p] path

Which, path for the directory you want to create, you can use absolute paths (beginning with /) or relative path ;-p is to create a multi-level directory. For example, to create in the current directory / animal / dog directory, if only enter mkdir animal / dog, it will return an error message: Unable to create directory, no such file or directory. The reason is that there is no current directory / animal at this level directory, create two directories are now equivalent to a one-time, so use mkdir -p animal / dog. Note that at this time can not be used mkdir -p / animal / dog, because this is the absolute path, meaning this is to create two directories in the root directory.

5. Delete empty directory

rmdir path

Where path is the directory to be deleted, this command can only delete empty directories that under no subdirectories and files; the second command to remove empty or non-empty directory.

6. Delete a file or directory

rm [-r] [-f] source

Which is the source you want to delete a file or directory, when it is a directory, you must add -r. -f means force the removal of not prompt.

7. Create an empty file

touch file1 [file2 file3  ...]

Where file1 is the file name, the command supports create multiple files, an intermediate file name with a space () intervals. For example, touch hello.txt abc.txt, that is, create two empty files in the current directory.

8. Copy the file or directory

cp [-r] source dest

Which, source for replication of a file or directory, when it is a directory, you need to add -r, said that "recursive copy the entire directory" file when it is, do not need to add -r, added no effect; desc is to be copied to the target directory, you can use absolute or relative path. For example, now you want to /document/aaa.txt to copy the current directory under / file directory under the current directory, use the cp document / aaa.txt file; and if you want to / document directory under the current directory entire replication to the next / file directory, use cp -r document file.

9. The mobile file or directory, rename

mv source dest

This command has two functions: move, rename.

When used as a mobile, source of the file or directory to be moved, dest target directory;

When used as rename, source and dest are either files or directories are, and the same position.

10. Open the file

cat [-n] file [|more]

Where, file is the file to open. The difference with the command vim is open read-only files can not be edited. -n means to display line numbers; | more mean pagination, his party turned down the Enter key, the spacebar to turn one down, q to quit.

more file

其中,file为要打开的文件。这个命令会以全屏分页的方式显示文件,回车翻一行,空格翻一页,q键退出。

less file

其中,file为要打开的文件。这个命令跟more很相似,区别是不会一次性加载整个文件,而是每次加载要显示的部分,空格翻页,q键退出。

11.输出内容到控制台

echo content

其中,content是要输出的内容;特别的,当content=$PATH时,会输出linux的环境变量。

12.覆盖文件或追加内容

>:用内容覆盖文件,即替代文件原来的内容

>>:保留文件原来的内容,将内容追加到文件末尾

这两个符号通常跟别的命令合用,以确定上面所说的"内容",例如:

ls -l > file

ls -l >> file

其中,file为文件,这个命令会将当前目录下的文件以列表的形式写到文件中;

cat source > file

cat source >> file

其中,source为源文件,file为目标文件,这个命令会将source文件的内容写到file文件中。

echo content > file

echo content >> file

其中,source为内容,file为目标文件,这个命令会将content写到文件中。

这两个符号还可以跟其他的命令合用,总之就是用前面的命令产生"内容",然后用内容替代文件或追加到文件末尾。

13.显示文件开头

head [-n numberfile

其中,number为显示的行数,file为要显示的文件,如果不加-n选项,则默认显示前10行。

14.显示文件末尾

tail [-n numberfile

其中,number为显示的行数,file为要显示的文件,如果不加-n选项,则默认显示末尾10行。

15.监控文件

tail -f file

其中,file为要监控的文件,当该文件有最新更新时,会实时显示,ctrl+c退出。

16.创建快捷方式

ln -s target name

其中,target为快捷方式要指向的文件或目录,name为快捷方式名称。例如,在当前用户的家目录下要建立到/etc/X11/applnk目录的快捷方式,则使用命令:ln -s /etc/X11/applnk query,然后cd query,就进入到指定目录下。要删除快捷方式,按照删除文件或目录的方式即可,即调用rm命令。

17.查找文件或目录

find path [-name] name

按名称查找,其中path为查找范围目录,name为文件或目录的名称,可以使用通配符,例如*.txt,hello.*。

find path [-size] capacity

按大小查找,其中path为查找范围目录,capacity为容量,例如+20M表示超过20M,-100k为小于100k。

find path [-user] username

按所属用户查找,其中path为查找范围目录,user为用户名。

18.在文件中查找内容

grep [-n] [-i] content file

其中,content为要查找的内容,file为文件,-n表示显示行号,-i表示忽略大小写。例如,要在当前目录的aaa.txt文件中查找may,显示行号且忽略大小写,调用命令:grep -ni may aaa.txt。

另外,grep命令经常跟管道符合用,例如:

cat file | grep [-n] [-i] content

管道符的作用是将第一个命令的处理结果,作为第二个命令的参数。

Guess you like

Origin www.cnblogs.com/dubhlinn/p/11031051.html