引言:你可能已经听说过在Linux中一切皆是文件,所以一定要打好基础,把文件和目录的管理如查找、列出、移动、复制、打包等学扎实。本文针对LPI 101考试,当然读者也可以为了兴趣来学习。
摘要:本文介绍几本的文件和目录管理命令,具体有
[root@localhost lpi103-2]# pwd
/root/lpi103-2
[root@localhost lpi103-2]# ls
msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
[root@localhost lpi103-2]# ls $PWD
msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
[root@localhost lpi103-2]# ls /root/lpi103-2/
msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
[root@localhost lpi103-2]# ls -al
total 92
drwxr-xr-x 2 root root 4096 May 7 17:29 .
dr-xr-x---. 10 root root 4096 May 7 15:47 ..
-rw-r--r-- 1 root root 43851 May 7 15:38 msg
-rw-r--r-- 1 root root 8 May 7 17:14 sedtab
-rw-r--r-- 1 root root 24 May 7 14:29 text1
-rw-r--r-- 1 root root 25 May 7 14:48 text2
-rw-r--r-- 1 root root 63 May 7 16:07 text3
-rw-r--r-- 1 root root 24 May 7 16:41 text5
-rw-r--r-- 1 root root 98 May 7 17:29 text6
-rw-r--r-- 1 root root 15 May 7 15:15 xaa
-rw-r--r-- 1 root root 9 May 7 15:15 xab
-rw-r--r-- 1 root root 17 May 7 15:16 yaa
-rw-r--r-- 1 root root 8 May 7 15:16 yab
[root@localhost lpi103-2]# ls -i
1839215 msg 1839209 text1 1839189 text3 1839219 text6 1839212 xab 1839214 yab
1839218 sedtab 1839210 text2 1839216 text5 1839211 xaa 1839213 yaa
ls 可以同时接受多个文件或目录作为参数。对于目录名,ls默认会读取目录的内容然后列出每个项的元信息,而不是列出目录本身的元信息。可以使用-d选项来改变这种默认方式。如下:
[root@localhost lpi103-2]# ls -ld ../lpi103-2 sedtab xaa
drwxr-xr-x 2 root root 4096 May 7 17:29 ../lpi103-2
-rw-r--r-- 1 root root 8 May 7 17:14 sedtab
-rw-r--r-- 1 root root 15 May 7 15:15 xaa
默认情况下,ls按照文件名排序输出。ls提供了很多选项来控制输出的排序,例如-t 是按照修改时间(最新修改在前)排序,-lS 则按照大小排序, -r则倒转排序先后。例如 -lrt会按照修改时间排序,并且最新修改的在最后输出。更多地选项参考man手册页。
我们已经学会了一些创建文件的方法,那么如何复制、重命名、移动、或者删除文件呢?这要用到三个很短的命令:
[root@localhost lpi103-2]# cp text1 text1.bkp
[root@localhost lpi103-2]# mkdir backup
[root@localhost lpi103-2]# cp text1 backup/text1.bkp.2
[root@localhost lpi103-2]# ls -i text1 text1.bkp backup/
1839209 text1 1839220 text1.bkp
backup/:
1839222 text1.bkp.2
[root@localhost lpi103-2]# mv text1.bkp backup
[root@localhost lpi103-2]# mv backup/text1.bkp backup/text1.bkp.1
[root@localhost lpi103-2]# ls -i text1 text1.bkp backup
ls: cannot access text1.bkp: No such file or directory
1839209 text1
backup:
1839220 text1.bkp.1 1839222 text1.bkp.2
[root@localhost lpi103-2]# cp text2 backup
[root@localhost lpi103-2]# cp --backup=t text2 backup
cp: overwrite `backup/text2'? y
[root@localhost lpi103-2]# ls backup/
text1.bkp.1 text1.bkp.2 text2 text2.~1~
[root@localhost lpi103-2]# mkdir dir1 dir2
[root@localhost lpi103-2]# ls
backup dir1 dir2 msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
[root@localhost lpi103-2]# mkdir d1/d2/d3
mkdir: cannot create directory `d1/d2/d3': No such file or directory
[root@localhost lpi103-2]# echo $?
1
[root@localhost lpi103-2]# mkdir -p d1/d2/d3
[root@localhost lpi103-2]# echo $?
0
[root@localhost lpi103-2]# cp text1 d1/d2/
[root@localhost lpi103-2]# rmdir -p d1/d2/d3 dir1 dir2
rmdir: failed to remove directory `d1/d2': Directory not empty
[root@localhost lpi103-2]# ls ./d1/d2
text1
[root@localhost lpi103-2]# rm d1/d2/text1
rm: remove regular file `d1/d2/text1'? y
[root@localhost lpi103-2]# rmdir -p d1/d2
[root@localhost lpi103-2]#
ls 使用-R选项来列出一个目录及其子目录。递归选项只作用于目录名。例如他不会找到所有的叫做text1的文件(译者注:没弄懂什么意思)。如下所示:
[root@localhost lpi103-2]# ls -iR
.:
1839221 backup 1839218 sedtab 1839210 text2 1839216 text5 1839211 xaa 1839213 yaa
1839215 msg 1839209 text1 1839189 text3 1839219 text6 1839212 xab 1839214 yab
./backup:
1839220 text1.bkp.1 1839222 text1.bkp.2 1839224 text2 1839223 text2.~1~
[root@localhost lpi103-2]# cp -pR . copy1
cp: cannot copy a directory, `.', into itself, `copy1'
[root@localhost lpi103-2]# ls -R
.:
backup copy1 msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
./backup:
text1.bkp.1 text1.bkp.2 text2 text2.~1~
./copy1:
backup msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
./copy1/backup:
text1.bkp.1 text1.bkp.2 text2 text2.~1~
[root@localhost lpi103-2]# rm -rf copy1
[root@localhost lpi103-2]# ls -R
.:
backup msg sedtab text1 text2 text3 text5 text6 xaa xab yaa yab
./backup:
text1.bkp.1 text1.bkp.2 text2 text2.~1~
[root@localhost lpi103-2]#
[root@localhost lpi103-2]# echo odd1>'text[*?!1]'
[root@localhost lpi103-2]# echo odd2>'text[2*?!]'
[root@localhost lpi103-2]# ls
backup msg sedtab text1 text[*?!1] text2 text[2*?!] text3 text5 text6 xaa xab yaa yab
[root@localhost lpi103-2]# ls text[2-4]
text2 text3
[root@localhost lpi103-2]# ls text[!2-4]
text1 text5 text6
[root@localhost lpi103-2]# ls text*[2-4]*
text2 text[2*?!] text3
[root@localhost lpi103-2]# ls text*[!2-4]*
text1 text[*?!1] text[2*?!] text5 text6
[root@localhost lpi103-2]# ls text*[!2-4]
text1 text[*?!1] text[2*?!] text5 text6
[root@localhost lpi103-2]# echo text*>text10
[root@localhost lpi103-2]# ls *\!*
text[*?!1] text[2*?!]
[root@localhost lpi103-2]# ls tex?[[]*
text[*?!1] text[2*?!]
[root@localhost lpi103-2]# rm tex?[[]*
rm: remove regular file `text[*?!1]'? y
rm: remove regular file `text[2*?!]'? y
[root@localhost lpi103-2]# ls *b*
sedtab xab yab
backup:
text1.bkp.1 text1.bkp.2 text2 text2.~1~
[root@localhost lpi103-2]# ls backup/*2
backup/text1.bkp.2 backup/text2
[root@localhost lpi103-2]# ls -d .*
[root@localhost lpi103-2]# echo text*
text1 text10 text2 text3 text5 text6
[root@localhost lpi103-2]# echo "text*"
text*
[root@localhost lpi103-2]# echo text[[\!?]z??
text[[!?]z??
[root@localhost lpi103-2]# echo xxx>f1; ls -l f1; sleep 60; touch f1; ls -l f1
-rw-r--r-- 1 root root 4 May 8 16:50 f1
-rw-r--r-- 1 root root 4 May 8 16:51 f1
[root@localhost lpi103-2]# touch f2; touch -c f3; ls -l f*
-rw-r--r-- 1 root root 4 May 8 16:51 f1
-rw-r--r-- 1 root root 0 May 8 16:53 f2
[root@localhost lpi103-2]# touch -t 198111261212.59 f3
[root@localhost lpi103-2]# touch -d 11am f4
[root@localhost lpi103-2]# touch -d "last fortnight" f5
[root@localhost lpi103-2]# touch -d "yesterday 6am" f6
[root@localhost lpi103-2]# touch -d "2 days ago 12:00" f7
[root@localhost lpi103-2]# touch -d "tomorrow 02:00" f8
[root@localhost lpi103-2]# touch -d "5 Nov" f9
[root@localhost lpi103-2]# ls -lrt f*
-rw-r--r-- 1 root root 0 Nov 26 1981 f3
-rw-r--r-- 1 root root 0 Apr 24 16:57 f5
-rw-r--r-- 1 root root 0 May 6 12:00 f7
-rw-r--r-- 1 root root 0 May 7 06:00 f6
-rw-r--r-- 1 root root 0 May 8 11:00 f4
-rw-r--r-- 1 root root 4 May 8 16:51 f1
-rw-r--r-- 1 root root 0 May 8 16:53 f2
-rw-r--r-- 1 root root 0 May 9 2013 f8
-rw-r--r-- 1 root root 0 Nov 5 2013 f9
[root@localhost lpi103-2]# date
Wed May 8 17:02:57 CST 2013
[root@localhost lpi103-2]# date -r f1
Wed May 8 16:51:50 CST 2013
[root@localhost lpi103-2]# touch -r f1 f1a
[root@localhost lpi103-2]# ls -l f1*
-rw-r--r-- 1 root root 4 May 8 16:51 f1
-rw-r--r-- 1 root root 0 May 8 16:51 f1a
[root@localhost lpi103-2]# cat f1
xxx
[root@localhost lpi103-2]# ls -lu f1*
-rw-r--r-- 1 root root 4 May 9 09:34 f1
-rw-r--r-- 1 root root 0 May 8 16:51 f1a
[root@localhost lpi103-2]# touch -a -r f1a f1
[root@localhost lpi103-2]# ls -lu f1*
-rw-r--r-- 1 root root 4 May 8 16:51 f1
-rw-r--r-- 1 root root 0 May 8 16:51 f1a
[root@localhost lpi103-2]# find . -name "*[1k]*"
./f1
./text10
./backup
./backup/text2.~1~
./backup/text1.bkp.1
./backup/text1.bkp.2
./f1a
./text1
[root@localhost lpi103-2]# find . -ipath "*ACK*1"
./backup/text1.bkp.1
[root@localhost lpi103-2]# find . -ipath "*ACK*/*1"
./backup/text1.bkp.1
[root@localhost lpi103-2]# find . -type d
.
./backup
[root@localhost lpi103-2]# find . -type d -name "*"
.
./backup
[root@localhost lpi103-2]# find . -size 0
./f8
./f6
./f9
./f2
./f5
./f1a
./f4
./f7
./f3
[root@localhost lpi103-2]# find . -size +23c -size -26c -print
./backup/text2.~1~
./backup/text1.bkp.1
./backup/text1.bkp.2
./backup/text2
./text2
./text1
./text5
root@localhost lpi103-2]# find . -size -26c -size +23c -ls
1839223 4 -rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2.~1~
1839220 4 -rw-r--r-- 1 root root 24 May 8 14:40 ./backup/text1.bkp.1
1839222 4 -rw-r--r-- 1 root root 24 May 8 14:41 ./backup/text1.bkp.2
1839224 4 -rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2
1839210 4 -rw-r--r-- 1 root root 25 May 7 14:48 ./text2
1839209 4 -rw-r--r-- 1 root root 24 May 7 14:29 ./text1
1839216 4 -rw-r--r-- 1 root root 24 May 7 16:41 ./text5
[root@localhost lpi103-2]# find . -size -26c -size +23c -exec ls -l '{}' \;
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2.~1~
-rw-r--r-- 1 root root 24 May 8 14:40 ./backup/text1.bkp.1
-rw-r--r-- 1 root root 24 May 8 14:41 ./backup/text1.bkp.2
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2
-rw-r--r-- 1 root root 25 May 7 14:48 ./text2
-rw-r--r-- 1 root root 24 May 7 14:29 ./text1
-rw-r--r-- 1 root root 24 May 7 16:41 ./text5
[root@localhost lpi103-2]# find . -mtime -2 -type f -exec ls -l '{}' \;
-rw-r--r-- 1 root root 0 May 9 02:00 ./f8
-rw-r--r-- 1 root root 8 May 7 15:16 ./yab
-rw-r--r-- 1 root root 0 Nov 5 2013 ./f9
-rw-r--r-- 1 root root 9 May 7 15:15 ./xab
-rw-r--r-- 1 root root 43851 May 7 15:38 ./msg
-rw-r--r-- 1 root root 0 May 8 16:53 ./f2
-rw-r--r-- 1 root root 4 May 8 16:51 ./f1
-rw-r--r-- 1 root root 17 May 7 15:16 ./yaa
-rw-r--r-- 1 root root 52 May 8 16:10 ./text10
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2.~1~
-rw-r--r-- 1 root root 24 May 8 14:40 ./backup/text1.bkp.1
-rw-r--r-- 1 root root 24 May 8 14:41 ./backup/text1.bkp.2
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2
-rw-r--r-- 1 root root 98 May 7 17:29 ./text6
-rw-r--r-- 1 root root 0 May 8 16:51 ./f1a
-rw-r--r-- 1 root root 25 May 7 14:48 ./text2
-rw-r--r-- 1 root root 0 May 8 11:00 ./f4
-rw-r--r-- 1 root root 24 May 7 14:29 ./text1
-rw-r--r-- 1 root root 24 May 7 16:41 ./text5
-rw-r--r-- 1 root root 8 May 7 17:14 ./sedtab
-rw-r--r-- 1 root root 63 May 7 16:07 ./text3
-rw-r--r-- 1 root root 15 May 7 15:15 ./xaa
[root@localhost lpi103-2]# find . -daystart -mtime -2 -type f -exec ls -l '{}' \;
-rw-r--r-- 1 root root 0 May 9 02:00 ./f8
-rw-r--r-- 1 root root 0 Nov 5 2013 ./f9
-rw-r--r-- 1 root root 0 May 8 16:53 ./f2
-rw-r--r-- 1 root root 4 May 8 16:51 ./f1
-rw-r--r-- 1 root root 52 May 8 16:10 ./text10
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2.~1~
-rw-r--r-- 1 root root 24 May 8 14:40 ./backup/text1.bkp.1
-rw-r--r-- 1 root root 24 May 8 14:41 ./backup/text1.bkp.2
-rw-r--r-- 1 root root 25 May 8 14:58 ./backup/text2
-rw-r--r-- 1 root root 0 May 8 16:51 ./f1a
-rw-r--r-- 1 root root 0 May 8 11:00 ./f4
[root@localhost lpi103-2]# find . -mmin -600 -mmin +60 -type f -exec ls -l '{}' \;
-rw-r--r-- 1 root root 0 May 9 02:00 ./f8
[root@localhost lpi103-2]# file backup/ text1 f2 ../music/wee-willie-winkie.mp3 /bin/echo
backup/: directory
text1: ASCII text
f2: empty
../music/wee-willie-winkie.mp3: Audio file with ID3 version 2.3.0, contains:
/bin/echo: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[root@localhost lpi103-2]# file -i backup/ text1 f2 ../music/wee-willie-winkie.mp3 /bin/echo
backup/: application/x-directory; charset=binary
text1: text/plain; charset=us-ascii
f2: application/x-empty; charset=binary
../music/wee-willie-winkie.mp3: application/octet-stream; charset=binary
/bin/echo: application/x-executable; charset=binary
[root@localhost lpi103-2]# cp -p /etc/services .
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# gzip services
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 127223 Oct 15 2012 services.gz
[root@localhost lpi103-2]# gzip -d services.gz
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# bzip2 services
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 115774 Oct 15 2012 services.bz2
[root@localhost lpi103-2]# bunzip2 services.bz2
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# gzip -N services
[root@localhost lpi103-2]# touch services.gz
[root@localhost lpi103-2]# mv services.gz services-x.gz
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 127223 May 9 11:15 services-x.gz
[root@localhost lpi103-2]# gzip -l services-x.gz
compressed uncompressed ratio uncompressed_name
127223 641020 80.2% services-x
[root@localhost lpi103-2]# gzip -lN services-x.gz
compressed uncompressed ratio uncompressed_name
127223 641020 80.2% services
[root@localhost lpi103-2]# gunzip -N services-x.gz
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 Oct 15 2012 services
[root@localhost lpi103-2]# bzip2 services
[root@localhost lpi103-2]# mv services.bz2 services-x.bz2
[root@localhost lpi103-2]# touch services-x.bz2
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 115774 May 9 11:17 services-x.bz2
[root@localhost lpi103-2]# bunzip2 services-x.bz2
[root@localhost lpi103-2]# ls -l serv*
-rw-r--r-- 1 root root 641020 May 9 11:17 services-x
[root@localhost lpi103-2]# rm services-x # Don't need this any more
[root@localhost lpi103-2]# tar -cvf ../lpitar1.tar .
./
./f8
./f6
./yab
./f9
./xab
./msg
./f2
./f5
./f1
./yaa
./text10
./backup/
./backup/text2.~1~
./backup/text1.bkp.1
./backup/text1.bkp.2
./backup/text2
./text6
./f1a
./text2
./f4
./text1
./text5
./sedtab
./f7
./text3
./xaa
./f3
[root@localhost lpi103-2]# tar -zcvf ../lpitar3.tar ~/lpi103-2/
tar: Removing leading `/' from member names
/root/lpi103-2/
/root/lpi103-2/f8
/root/lpi103-2/f6
/root/lpi103-2/yab
/root/lpi103-2/f9
/root/lpi103-2/xab
/root/lpi103-2/msg
/root/lpi103-2/f2
/root/lpi103-2/f5
/root/lpi103-2/f1
/root/lpi103-2/yaa
/root/lpi103-2/text10
/root/lpi103-2/backup/
/root/lpi103-2/backup/text2.~1~
/root/lpi103-2/backup/text1.bkp.1
/root/lpi103-2/backup/text1.bkp.2
/root/lpi103-2/backup/text2
/root/lpi103-2/text6
/root/lpi103-2/f1a
/root/lpi103-2/text2
/root/lpi103-2/f4
/root/lpi103-2/text1
/root/lpi103-2/text5
/root/lpi103-2/sedtab
/root/lpi103-2/f7
/root/lpi103-2/text3
/root/lpi103-2/xaa
/root/lpi103-2/f3
[root@localhost lpi103-2]# ls -l ../lpitar*
-rw-r--r-- 1 root root 71680 May 9 13:26 ../lpitar1.tar
-rw-r--r-- 1 root root 13595 May 9 13:30 ../lpitar2.tar
[root@localhost lpi103-2]# touch f1
[root@localhost lpi103-2]# tar --diff --file ../lpitar1.tar .
./f1: Mod time differs
[root@localhost lpi103-2]# tar -df ../lpitar2.tar -C ./
./f1: Mod time differs
[root@localhost lpi103-2]# tar -xvf ../lpitar1.tar ./f1
./f1
[root@localhost lpi103-2]# tar --compare -f ../lpitar2.tar --directory ./
[root@localhost lpi103-2]# tar -tf ../lpitar1.tar "*f1*"
./f1
./f1a
[root@localhost lpi103-2]# find . -depth -print0 | cpio --null -o > ../lpicpio.1
89 blocks
[root@localhost lpi103-2]# find ~/lpi103-2/ -depth -print0 | cpio --null -o > ../lpicpio.2
90 blocks
[root@localhost lpi103-2]# cpio -i --list "*backup*"< ../lpicpio.1
backup/text2.~1~
backup/text1.bkp.1
backup/text1.bkp.2
backup/text2
backup
89 blocks
[root@localhost lpi103-2]# cpio -i --list absolute-filenames "*text1*" < ../lpicpio.2
/root/lpi103-2/text10
/root/lpi103-2/backup/text1.bkp.1
/root/lpi103-2/backup/text1.bkp.2
/root/lpi103-2/text1
90 blocks
[root@localhost lpi103-2]# mkdir temp
[root@localhost lpi103-2]# cd temp/
[root@localhost temp]# cpio -idv "*f1*" "*.bkp.1" <../../lpicpio.1
f1
backup/text1.bkp.1
f1a
89 blocks
[root@localhost temp]# cpio -idv "*.bkp.1" < ../../lpicpio.1
cpio: backup/text1.bkp.1 not created: newer or same age version exists
backup/text1.bkp.1
89 blocks
[root@localhost temp]# cpio -id --no-absolute-filenames "*text1*" < ../../lpicpio.2
cpio: Removing leading `/' from member names
90 blocks
[root@localhost temp]# cd ..
[root@localhost lpi103-2]# rm -rf temp
[root@localhost lpi103-2]# cat text6
1 apple
2 pear
3 banana
9 plum
3 banana
10 apple
1 apple
2 pear
3 banana
9 plum
3 banana
10 apple
[root@localhost lpi103-2]# dd if=text6 conv=ucase ibs=20 obs=30
1 APPLE
2 PEAR
3 BANANA
9 PLUM
3 BANANA
10 APPLE
1 APPLE
2 PEAR
3 BANANA
9 PLUM
3 BANANA
10 APPLE
4+1 records in
3+1 records out
98 bytes (98 B) copied, 3.9944e-05 s, 2.5 MB/s
[root@localhost ~]# dd if=/dev/sda6 of=backup-1
31248384+0 records in
31248384+0 records out
15999172608 bytes (16 GB) copied, 855.25 s, 18.7 MB/s
[root@localhost ~]# ll
total 15624432
-rw-------. 1 root root 1033 Mar 27 11:12 anaconda-ks.cfg
-rw-r--r-- 1 root root 15999172608 May 9 15:17 backup-1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 56G 18G 35G 34% /
tmpfs 947M 0 947M 0% /dev/shm
/dev/sda5 74G 15G 56G 21% /data
/dev/sda6 15G 870M 14G 7% /work
[root@localhost ~]# dd if=/dev/sda6 | gzip >backup-2
31248384+0 records in
31248384+0 records out
15999172608 bytes (16 GB) copied, 1549.11 s, 10.3 MB/s
[root@localhost ~]# ll back*
-rw-r--r-- 1 root root 15999172608 May 9 15:17 backup-1
-rw-r--r-- 1 root root 13400290321 May 9 15:55 backup-2
译者注:本实验译者没做,没找到合适的移动硬盘。