博客园不常在线

有问题联系微信

微信号

微信公众号

Linux系列:文件相关操作

1.查看文件

cat:查看小文件

more:按空格翻页查看;按Enter行查看;按Q退出

less:按空格翻页查看;按Enter行查看;P往回看;F往后看

head:查看前几行,默认前十行

tail:查看尾几行,默认尾部十行

 

 

 

[root@centos-master test]# less test.txt
[root@centos-master test]# less test.txt
[root@centos-master test]# head test.txt
test!
ot@centos-master ~]# history
    1  2020-10-25 21:03:39
    2  2020-09-17 20:43:52 ls[root@centos-master ~]# history
    1  2020-10-25 21:03:39
    2  2020-09-17 20:43:52 ls
    3  2020-09-17 20:43:54 ls
    4  2020-09-17 20:43:57 cd /
    5  2020-09-17 20:43:58 ls
    6  2020-09-17 20:50:31 cd /
[root@centos-master test]# tail test.txt
  simple, never   always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cp invocation'
[root@centos-master test]#

[root@centos-master test]# head -5 test.txt
test!
ot@centos-master ~]# history
    1  2020-10-25 21:03:39
    2  2020-09-17 20:43:52 ls[root@centos-master ~]# history
    1  2020-10-25 21:03:39
[root@centos-master test]# tail -1 test.txt

[root@centos-master test]#

2.移动、重命名:mv

[root@centos-master test]# mkdir testone
[root@centos-master test]# ls
testone
[root@centos-master test]# mv testone/ testtwo
[root@centos-master test]# ls
testtwo
[root@centos-master test]# mkdir dir1
[root@centos-master test]# mv testtwo/ dir1/
[root@centos-master test]# tree
.
`-- dir1
    `-- testtwo

2 directories, 0 files
[root@centos-master test]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --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
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'mv invocation'
[root@centos-master test]#

3.硬链接、软连接:ln

ln file1 file2   硬链接

ln -s file1 file2  软链接

目录是不能创建硬链接的

[root@centos-master test]# vim hello.c
[root@centos-master test]# ls
hello.c
[root@centos-master test]# cat hello.c
hello world!
[root@centos-master test]# ln hello.c  hello2.c
[root@centos-master test]# ls
hello2.c  hello.c
[root@centos-master test]# cat hello2.c
hello world!
[root@centos-master test]# ls -l
total 8
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello2.c
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
[root@centos-master test]# touch test.txt
[root@centos-master test]# ls -l
total 8
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello2.c
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
-rw-r--r-- 1 root root  0 Dec  8 21:29 test.txt
[root@centos-master test]# ln hello.c hello3.c
[root@centos-master test]# ls -l
total 12
-rw-r--r-- 3 root root 13 Dec  8 21:27 hello2.c
-rw-r--r-- 3 root root 13 Dec  8 21:27 hello3.c
-rw-r--r-- 3 root root 13 Dec  8 21:27 hello.c
-rw-r--r-- 1 root root  0 Dec  8 21:29 test.txt
[root@centos-master test]# rm  hello3.c
rm: remove regular file ‘hello3.c’? y
[root@centos-master test]# ls -l
total 8
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello2.c
-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
-rw-r--r-- 1 root root  0 Dec  8 21:29 test.txt
[root@centos-master test]# cat hello2.c
hello world!
[root@centos-master test]# vim hello2.c
[root@centos-master test]# cat hello.c
test hello world!
[root@centos-master test]# cat hello2.c
test hello world!
[root@centos-master test]#

4.查看文件信息:wc、od、du、df

od:查看二进制文件的信息

wc:查看普通文件的信息

第一个代表的是行 第二代表的是数目 以空格为分  第三个代表的是字节数

[root@centos-master test]# wc test.txt
 2  9 19 test.txt
[root@centos-master test]# cat test.txt
1 2 3 4 55 6
2 2 3

du:查看当前目录的信息 加上-h才能看懂

[root@centos-master test]# du
8       .
[root@centos-master test]# tree
.
`-- test.txt

0 directories, 1 file
[root@centos-master test]# du -h
8.0K    .
[root@centos-master test]#

df:磁盘的使用量

[root@centos-master test]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          929552       0    929552   0% /dev
tmpfs             941072      48    941024   1% /dev/shm
tmpfs             941072     732    940340   1% /run
tmpfs             941072       0    941072   0% /sys/fs/cgroup
/dev/vda1       51473868 6327512  42949112  13% /
tmpfs             188216       0    188216   0% /run/user/0
[root@centos-master test]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        908M     0  908M   0% /dev
tmpfs           920M   48K  919M   1% /dev/shm
tmpfs           920M  732K  919M   1% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
/dev/vda1        50G  6.1G   41G  13% /
tmpfs           184M     0  184M   0% /run/user/0
[root@centos-master test]#

5.命令解析器:which

只能查看一些外部的命令

[root@centos-master test]# which cp
alias cp='cp -i'
        /usr/bin/cp
[root@centos-master test]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@centos-master test]# which cd
/usr/bin/cd
[root@centos-master test]#

 

posted @ 2020-12-08 21:57  Code技术分享  阅读(0)  评论(0编辑  收藏  举报