Linux directory and file management

Excerpt from rookie tutorial for you to remember with

  • Linux directory structure as a tree structure, the top of the directory is the root directory /.

  • Absolute path:
    the wording path from the root directory / write from, for example: / usr / share / doc directory. (pwd view the current directory)
  • Relative path:
    writing path, not by / write from, for example, the / usr / share / doc when you want to / usr / share / under man, can be written as: cd ../man This is the wording of the relative path of it! ./ in the relative path is switched to the catalog, .. / are switched to a directory

Common command processing directory

  • By (mkdir to create, cp copy)
  • Deleted (rmdir to delete empty directories, rm (delete a directory or file))
  • Change mv (move files and directories, or modify the name)
  • Charles ls

You can use the man [command] to view the document using the individual commands, such as: man cp.

ls

-a :全部的文件,连同隐藏档( 开头为 . 的文件) 一起列出来(常用)
-d :仅列出目录,而不列出目录内的文件数据(常用)
-l :长数据串列出,包含文件的属性与权限等等数据;(常用)

mkdir (create a new directory)

  • -m: Oh permissions profile! Direct configuration, do not need to see the default permissions (umask) face ~
  • -p: help you directly to the required directory (containing the parent directory) recursively create up!
[root@www ~]# cd /tmp
[root@www tmp]# mkdir test    <==创建一名为 test 的新目录
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4': 
No such file or directory       <== 没办法直接创建此目录啊!
[root@www tmp]# mkdir -p test1/test2/test3/test4

Example: Create permissions 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

rmdir (remove empty directories)

 rmdir [-p] 目录名称   
 -p连同上一级『空的』目录也一起删除

Delete runoob directory

[root@www tmp]# rmdir runoob/

The directory (/ tmp under) mkdir instance created deleted!

[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
[root@www tmp]# rmdir test   <==可直接删除掉,没问题
[root@www tmp]# rmdir test1  <==因为尚有内容,所以无法删除!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l        <==您看看,底下的输出中test与test1不见了!
drwx--x--x  2 root  root 4096 Jul 18 12:54 test2
利用 -p 这个选项,立刻就可以将 test1/test2/test3/test4 一次删除。
不过要注意的是,这个 rmdir 仅能删除空的目录,你可以使用 rm 命令来删除非空目录。

cp (copy a file or directory)

cp [-adfilprsu] 来源档(source) 目标档(destination)
cp [options] source1 source2 source3 .... directory
-a:相当於 -pdr 的意思,至於 pdr 请参考下列说明;(常用)
-i:若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
-p:连同文件的属性一起复制过去,而非使用默认属性(备份常用);
-r:递归持续复制,用於目录的复制行为;(常用)
-u:若 destination 比 source 旧才升级 destination !

Copy as root .bashrc under the root directory to / tmp, and was named bashrc

[root@www ~]# cp ~/.bashrc /tmp/bashrc
[root@www ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite `/tmp/bashrc'? n  <==n不覆盖,y为覆盖

rm (remove file or directory)

rm [-fir] 文件或目录
- -f :就是 force 的意思,忽略不存在的文件,不会出现警告信息;
- -i :互动模式,在删除前会询问使用者是否动作
- -r :递归删除啊!最常用在目录的删除了!这是非常危险的选项!!!

The bashrc just created in the instance of the cp deleted!

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

If we add -i option will take the initiative to ask Oh, you avoid deleting the wrong file name!

mv (move files and directories, or modify the name)

[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
- -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
- -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
- -u :若目标文件已经存在,且 source 比较新,才会升级 (update)

Copy a file, create a directory, move the file to a directory

[root@www ~]# cd /tmp
[root@www tmp]# cp ~/.bashrc bashrc
[root@www tmp]# mkdir mvtest
[root@www tmp]# mv bashrc mvtest
将某个文件移动到某个目录去,就是这样做!

The directory name just renamed mvtest2

[root@www tmp]# mv mvtest mvtest2

Linux file contents View

You can use the man [command] to view the document using the individual commands, such as: man cp.

cat from the first line displays the file contents

cat [-AbEnTv]
- -n :列印出行号,连同空白行也会有行号,与 -b 的选项不同;
- -b :列出行号,仅针对非空白行做行号显示,空白行不标行号! 

nl display line numbers

nl [-bnw] 文件
- -b :指定行号指定的方式,主要有两种:
  -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
  -b t :如果有空行,空的那一行不要列出行号(默认值,类似 cat -b);
- -n :列出行号表示的方法,主要有三种:
  -n ln :行号在荧幕的最左方显示;  -n rn :行号在自己栏位的最左方显示,且不加 0 ;
  -n rz :行号在自己栏位的最左方显示,且加 0 ;
- -w :行号栏位的占用的位数。(没试出来)

more / less flip from page to page

- 空白键 (space):代表向下翻一页;
- Enter         :代表向下翻『一行』;
- [pagedown]:向下翻动一行;[pageup]  :向上翻动一行;
- q         :离开  这个程序;
- b 或 [ctrl]-b :代表往回翻页,不过这动作只对文件有用,对管线无用。
- /字串         :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
- :f            :立刻显示出档名以及目前显示的行数;

- /字串     :向下搜寻『字串』的功能;
- ?字串     :向上搜寻『字串』的功能;
- n         :重复前一个搜寻 (与 / 或 ? 有关!)
- N         :反向的重复前一个搜寻 (与 / 或 ? 有关!)

Before the head / tail out files / back lines

head/tail [-n number] 文件 
-n :后面接数字,代表显示几行的意思,默认的情况中,显示前面10行!若要显示前20行,就得要这样:
[root@www ~]# head -n 20 /etc/man.config

Guess you like

Origin www.cnblogs.com/jiangxiangxiang/p/10960548.html