Linux 环境中支持输入输出重定向,用符号来表示。 将当前目录下的文件以长模式显示,然后写入 listfile 文件:
[root@localhost ~]# ls -l > listfile
[root@localhost ~]# cat listfile
total 12
drwxr-xr-x. 2 root root 6 Jul 19 10:52 -
-rw-------. 1 root root 2262 Sep 2 2021 anaconda-ks.cfg
drwxr-xr-x. 2 root root 22 Jul 20 13:14 Desktop
drwxr-xr-x. 2 root root 6 Jul 19 10:51 dirtest
drwxr-xr-x. 2 root root 6 Sep 2 2021 Documents
drwxr-xr-x. 2 root root 6 Sep 2 2021 Downloads
-rw-r--r--. 1 root root 2310 Sep 2 2021 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Aug 14 13:31 listfile
drwxr-xr-x. 2 root root 6 Sep 2 2021 Music
drwxr-xr-x. 3 root root 19 Jul 19 10:53 mytest
drwxr-xr-x. 2 root root 6 Jul 19 10:52 p
drwxr-xr-x. 2 root root 4096 Jul 19 12:58 Pictures
drwxr-xr-x. 2 root root 6 Sep 2 2021 Public
drwxr-xr-x. 2 root root 6 Sep 2 2021 Templates
drwxrwxrwx. 2 root root 6 Jul 19 10:54 testmod
drwxr-xr-x. 3 root root 17 Jul 21 09:26 u02
drwxr-xr-x. 2 root root 6 Sep 2 2021 Videos
[root@localhost ~]#
使用>进行输出重定向,文件的原内容会被覆盖,可以使用>>将输出追加入文件:
[root@localhost ~]# ll /home/oracle >> listfile
[root@localhost ~]# cat listfile
total 12
drwxr-xr-x. 2 root root 6 Jul 19 10:52 -
-rw-------. 1 root root 2262 Sep 2 2021 anaconda-ks.cfg
drwxr-xr-x. 2 root root 22 Jul 20 13:14 Desktop
drwxr-xr-x. 2 root root 6 Jul 19 10:51 dirtest
drwxr-xr-x. 2 root root 6 Sep 2 2021 Documents
drwxr-xr-x. 2 root root 6 Sep 2 2021 Downloads
-rw-r--r--. 1 root root 2310 Sep 2 2021 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Aug 14 13:31 listfile
drwxr-xr-x. 2 root root 6 Sep 2 2021 Music
drwxr-xr-x. 3 root root 19 Jul 19 10:53 mytest
drwxr-xr-x. 2 root root 6 Jul 19 10:52 p
drwxr-xr-x. 2 root root 4096 Jul 19 12:58 Pictures
drwxr-xr-x. 2 root root 6 Sep 2 2021 Public
drwxr-xr-x. 2 root root 6 Sep 2 2021 Templates
drwxrwxrwx. 2 root root 6 Jul 19 10:54 testmod
drwxr-xr-x. 3 root root 17 Jul 21 09:26 u02
drwxr-xr-x. 2 root root 6 Sep 2 2021 Videos
total 0
[root@localhost ~]#
备注:可以使用 cat file1 > file2 命令实现文件 file1 到 file2 的拷贝。cat file* > file 可将数个小文件合并成一个大文件。