CentOS常用命令——文件操作

  • pwd

显示当前位置

# pwd
/opt
  • cd

切换目录

# cd nginx/
  • ls

显示目录列表

常用参数:
	-l 显示文件详细信息
	-a 显示所有文件,包括隐藏文件和文件夹
	-h 文件大小按照单位显示, K, M, G, T, P, E, Z, Y(按照1024进位); KB, MB, ...(按照1000进位)
	-R 递归列出所有子目录。
	-1 每行只列出一个文件
	-S 根据文件大小排序
	-X 根据扩展名排序
	-c 根据状态改变时间(ctime)排序
	-t 根据最后修改时间(mtime)排序
	-u 根据最后访问时间(atime)排序
	-r 依相反次序排列

基本用法:

# ls
data  log  nginx  redis  tmp

# ls -l
total 20
drwxr-xr-x 2 root root 4096 Mar 30 09:51 data
drwxr-xr-x 2 root root 4096 Mar 30 09:51 log
drwxr-xr-x 2 root root 4096 Mar 30 09:51 nginx
drwxr-xr-x 2 root root 4096 Mar 30 09:51 redis
drwxr-xr-x 2 root root 4096 Mar 30 09:51 tmp

# ls -lh
total 20K
drwxr-xr-x 2 root root 4.0K Mar 30 09:51 data
drwxr-xr-x 2 root root 4.0K Mar 30 09:51 log
drwxr-xr-x 2 root root 4.0K Mar 30 09:51 nginx
drwxr-xr-x 2 root root 4.0K Mar 30 09:51 redis
drwxr-xr-x 2 root root 4.0K Mar 30 09:51 tmp

# ls -lha
total 28K
drwxr-xr-x.  7 root root 4.0K Mar 30 10:11 .
dr-xr-xr-x. 20 root root 4.0K Jan 16 13:32 ..
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 data
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 log
-rw-r--r--   1 root root    0 Mar 30 10:11 .log
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 nginx
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 redis
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 tmp

文件单位:

# ls -alh  --block-size=K
total 28K
drwxr-xr-x.  7 root root 4K Mar 30 10:11 .
dr-xr-xr-x. 20 root root 4K Jan 16 13:32 ..
drwxr-xr-x   2 root root 4K Mar 30 09:51 data
drwxr-xr-x   2 root root 4K Mar 30 09:51 log
-rw-r--r--   1 root root 0K Mar 30 10:11 .log
drwxr-xr-x   2 root root 4K Mar 30 09:51 nginx
drwxr-xr-x   2 root root 4K Mar 30 09:51 redis
drwxr-xr-x   2 root root 4K Mar 30 09:51 tmp

# ls -alh  --block-size=G
total 1G
drwxr-xr-x.  7 root root 1G Mar 30 10:11 .
dr-xr-xr-x. 20 root root 1G Jan 16 13:32 ..
drwxr-xr-x   2 root root 1G Mar 30 09:51 data
drwxr-xr-x   2 root root 1G Mar 30 09:51 log
-rw-r--r--   1 root root 0G Mar 30 10:11 .log
drwxr-xr-x   2 root root 1G Mar 30 09:51 nginx
drwxr-xr-x   2 root root 1G Mar 30 09:51 redis
drwxr-xr-x   2 root root 1G Mar 30 09:51 tmp

# ls -alh  --block-size=GB
total 1GB
drwxr-xr-x.  7 root root 1GB Mar 30 10:11 .
dr-xr-xr-x. 20 root root 1GB Jan 16 13:32 ..
drwxr-xr-x   2 root root 1GB Mar 30 09:51 data
drwxr-xr-x   2 root root 1GB Mar 30 09:51 log
-rw-r--r--   1 root root 0GB Mar 30 10:11 .log
drwxr-xr-x   2 root root 1GB Mar 30 09:51 nginx
drwxr-xr-x   2 root root 1GB Mar 30 09:51 redis
drwxr-xr-x   2 root root 1GB Mar 30 09:51 tmp
				**可以发现单位是不靠谱的**

排序:

# ls -lha -t
total 28K
drwxr-xr-x.  7 root root 4.0K Mar 30 10:11 .
-rw-r--r--   1 root root    0 Mar 30 10:11 .log
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 log
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 data
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 tmp
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 nginx
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 redis
dr-xr-xr-x. 20 root root 4.0K Jan 16 13:32 ..

# ls -lha -t -r
total 28K
dr-xr-xr-x. 20 root root 4.0K Jan 16 13:32 ..
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 redis
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 nginx
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 tmp
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 data
drwxr-xr-x   2 root root 4.0K Mar 30 09:51 log
-rw-r--r--   1 root root    0 Mar 30 10:11 .log
drwxr-xr-x.  7 root root 4.0K Mar 30 10:11 .

# ls -aS
.  ..  data  log  nginx  redis  tmp  .log

# ls -aS -r
.log  tmp  redis  nginx  log  data  ..  .

递归文件夹:

# ls -R
.:
data  log  nginx  redis  tmp

./data:

./log:

./nginx:

./redis:

./tmp:



  • tree

显示文件和目录树

常用参数:

-a:显示所有文件和目录;
-A:使用ASNI绘图字符显示树状图而非以ASCII字符组合;
-C:在文件和目录清单加上色彩,便于区分各种类型;
-d:先是目录名称而非内容;
-D:列出文件或目录的更改时间;
-f:在每个文件或目录之前,显示完整的相对路径名称;
-F:在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","@","|"号;
-g:列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码;
-i:不以阶梯状列出文件和目录名称;
-l:<范本样式> 不显示符号范本样式的文件或目录名称;
-l:如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录;
-n:不在文件和目录清单加上色彩;
-N:直接列出文件和目录名称,包括控制字符;
-p:列出权限标示;
-P:<范本样式> 只显示符合范本样式的文件和目录名称;
-q:用“?”号取代控制字符,列出文件和目录名称;
-s:列出文件和目录大小;
-t:用文件和目录的更改时间排序;
-u:列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码;
-x:将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该目录予以排除在寻找范围外。
基本用法:
# tree
.
├── data
│   ├── k
│   ├── p
│   │   ├── p1
│   │   └── p2
│   └── service
├── log
├── nginx
├── redis
└── tmp

# tree /tmp
/tmp
├── hsperfdata_root
│   └── 86
├── systemd-private-b110814fff0f12b3851-chronyd.asMq
│   └── tmp
├── wrapper-666-1-in
└── wrapper-666-1-out

3 directories, 3 files

  • mkdir

功能:创建目录
选项:
  -m  set file mode (as in chmod), not a=rwx - umask
  -p  no error if existing, make parent directories as needed
  -v  print a message for each created directory
创建目录:
# mkdir data/service
同时创建多个目录:
# mkdir data/service data/p data/k data/p/p1 data/p/p2

  • rmdir

删除空目录
选项:
  -p  删除目录及其父目录
  -v  删除文件输出提示

基本用法:

# rmdir log

# rmdir log -v
rmdir: removing directory, ‘log’
rmdir: failed to remove ‘log’: No such file or directory

# rmdir -v -p log/d3/2019
rmdir: removing directory, ‘log/d3/2019’
rmdir: removing directory, ‘log/d3’
rmdir: removing directory, ‘log’
rmdir: failed to remove directory ‘log’: Directory not empty

  • touch

功能:生成新的空文件
选项:
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time

实例:

# touch log.log

#  ls -l *.log
-rw-r--r-- 1 root root 0 Mar 30 15:30 log.log

# touch log.log -m

#  ls -l *.log
-rw-r--r-- 1 root root 0 Mar 30 18:14 log.log

  • cp

功能:复制文件或目录
选项:
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
  -d                           same as --no-dereference --preserve=links
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (this option
                                 is ignored when the -n option is also used)
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber             do not overwrite an existing file (overrides
                                 a previous -i option)
  -P, --no-dereference         never follow symbolic links in SOURCE
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: context, links, xattr,
                                 all
  -c                           deprecated, same as --preserve=context
      --no-preserve=ATTR_LIST  don't preserve the specified attributes
      --parents                use full source file name under DIRECTORY
  -R, -r, --recursive          copy directories recursively
      --reflink[=WHEN]         control clone/CoW copies. See below
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -s, --symbolic-link          make symbolic links instead of copying
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 copy only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -x, --one-file-system        stay on this file system
  -Z                           set SELinux security context of destination
                                 file to default type

实例:

# cp log.log log1.log

# ls -l *.log
-rw-r--r-- 1 root root 0 Mar 30 18:18 log1.log
-rw-r--r-- 1 root root 0 Mar 30 18:14 log.log

  • mv

功能:移动文件或目录
选项:
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type

实例:

# mv a.bat b.dat -v -f
‘a.bat’ -> ‘b.dat’

  • rm

功能:删除文件或目录
选项:
  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root   do not remove '/' (default)
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done

实例:

# rm log log.log -f -v
rm: cannot remove ‘log’: Is a directory
removed ‘log.log’

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×