unix文件管理命令
unix系统提供了很多命令用于管理文件和目录。它们以简单的方式完成各种特定的任务,比如删除符合指定条件的文件,重命名某些文件。下面是一些常用到的命令。
1、ls
ls是一个使用频率非常高的命令,它指指定目录的内容输出到标准输出,如果没有指定目录则输出当前工作目录的内容。如下图:
-bash-3.00$ ls
di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000
-bash-3.00$ ls /
InstallShield bin dev etc home kernel
TT_DB core devices export install_tmp lib
第二个例子指定了“/”目录为输出目录,使用ls的“-l”参数能够输出更加详细的信息,如下所示:
-bash-3.00$ ls -l
total 80
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Apr 13 11:23 di.c
-rwxr-xr-x 1 bugboy 9178 16576 Apr 11 16:06 ethnet
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Apr 11 15:58 ethnet.h
-rwxr-xr-x 1 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
为了节省,我省略掉了部份输出内容,但是不影响讲解。第一行输出的是目录下的文件所占用的内存块数。随后的各行输出按从左到右的顺序分别为:
文件和目录的权限 到文件或者目录的硬连接数 文件的拥有者 文件所属的组 文件大小 最后一些修改的时间 文件/目录名
2、cd
cd(change directory)命令改变当前工作目录为指定目录。
-bash-3.00$ pwd
/home/b/o/bugboy
-bash-3.00$ cd /
-bash-3.00$ pwd
/
-bash-3.00$ cd ~
-bash-3.00$ pwd
/home/b/o/bugboy
-bash-3.00$ cd -
/
"cd ~"命令把当前工作目录切换到用户的主目录,"cd -"命令回到上一个历史工作目录,即执行上一次cd命令前的工作目录。
3、mkdir, rmdir
mkdir命令创建一个目录,rmdir删除一个空目录。
-bash-3.00$ ls
di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000
-bash-3.00$ mkdir test
-bash-3.00$ ls
di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000 test
在创建多层目录时,如果父目录不存在,可以给mkdir命令加“-p“参数创建父目录。
-bash-3.00$ ls
di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000 test
-bash-3.00$ mkdir -p a/b/c
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000 test
-bash-3.00$ ls -R
.:
a di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000 test
./a:
b
./a/b:
c
./a/b/c:
./t1000:
./test:
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000 test
-bash-3.00$ rmdir test
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h kstat kstat.c t1000
4、cp, mv, rm
cp (copy)命令,顾名思义就是复制文件/目录,cp命令有两个参数,一个是源文件/目录,一个是目标地址。
mv (move)命令,把文件/目录从一个地方移动到另一个地方,该命令也可以用来重命名文件/目录。
rm (remove)命令,删除文件/目录。
这三个命令都有以下参数表示相同的意思
-f force 强制移动文件,即使用户没有写权限
-i interactive 交互式,询问用户确认操作
-b backup 备份被覆盖的文件(仅对cp和mv命令)
-r/-R recursive 复制/删除整个目录(仅对cp和rm命令)
5、ln
ln命令创建一个链接(硬链接/符号链接)。
符号链接,指向一个文件,相当于一个文件索引,符号链接里保存它的源文件名。
硬链接,指向一个存放文件的物理磁盘空间。
-bash-3.00$ ln -s ethnet sys_link
-bash-3.00$ ls -l
total 84
drwxr-xr-x 3 bugboy 9178 4096 Sep 13 16:35 a
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Apr 13 11:23 di.c
-rwxr-xr-x 1 bugboy 9178 16576 Apr 11 16:06 ethnet
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Apr 11 15:58 ethnet.h
-rwxr-xr-x 1 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
lrwxrwxrwx 1 bugboy 9178 6 Sep 13 17:04 sys_link -> ethnet
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
-bash-3.00$ ln kstat hard_link
-bash-3.00$ ls -l
total 72
drwxr-xr-x 3 bugboy 9178 4096 Sep 13 16:35 a
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Apr 13 11:23 di.c
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Apr 11 15:58 ethnet.h
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 hard_link
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
lrwxrwxrwx 1 bugboy 9178 6 Sep 13 17:04 sys_link -> ethnet
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
删除一个符号链接指向的文件,符号链接并不会被删除,但是会使得符号链接失效。
删除一个硬链接指向的文件时,文件并不会被删除,当文件的硬链接数减为0的时候文件才会被删除。
6、touch
touch命令用于创建文件或者修改文件的最后修改时间。如果指定的文件存在,则修改文件的最后修改时间为当前的系统时间,如果指定的文件不存在,则创建文件。可以用-t参数来指定一个时间为文件的最后修改时间。如下所示:
-bash-3.00$ ls -l
total 72
drwxr-xr-x 3 bugboy 9178 4096 Sep 13 16:35 a
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Apr 13 11:23 di.c
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Apr 11 15:58 ethnet.h
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 hard_link
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
lrwxrwxrwx 1 bugboy 9178 6 Sep 13 17:04 sys_link -> ethnet
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
-bash-3.00$ touch di.c
-bash-3.00$ ls -l
total 72
drwxr-xr-x 3 bugboy 9178 4096 Sep 13 16:35 a
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Sep 13 17:16 di.c
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Apr 11 15:58 ethnet.h
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 hard_link
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
lrwxrwxrwx 1 bugboy 9178 6 Sep 13 17:04 sys_link -> ethnet
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
-bash-3.00$ touch ethnet.h -t 200709021200
-bash-3.00$ ls -l
total 72
drwxr-xr-x 3 bugboy 9178 4096 Sep 13 16:35 a
-rwxr-xr-x 1 bugboy 9178 7224 Apr 13 11:23 di
-rw-r--r-- 1 bugboy 9178 427 Sep 13 17:16 di.c
-rw-r--r-- 1 bugboy 9178 27030 Apr 11 15:58 ethnet.c
-rw-r--r-- 1 bugboy 9178 1234 Sep 2 12:00 ethnet.h
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 hard_link
-rwxr-xr-x 2 bugboy 9178 7496 Apr 11 09:38 kstat
-rw-r--r-- 1 bugboy 9178 815 Apr 11 09:37 kstat.c
lrwxrwxrwx 1 bugboy 9178 6 Sep 13 17:04 sys_link -> ethnet
drwx------ 5 bugboy 9178 4096 Apr 19 14:28 t1000
7、df, du
df和du显示磁盘的使用情况,df是对整个文件系统的,而du只对指定文件/目录。利用“-h”参数按人们比较容易理解的格式输出。
-bash-3.00$ du -h
56K ./.sunstudio/user_info
60K ./.sunstudio
52K ./t1000/.sunstudio/user_info
56K ./t1000/.sunstudio
8.0K ./t1000/.ssh
4.0K ./t1000/.sunw/pkcs11_softtoken/private
4.0K ./t1000/.sunw/pkcs11_softtoken/public
16K ./t1000/.sunw/pkcs11_softtoken
20K ./t1000/.sunw
104K ./t1000
8.0K ./.ssh
4.0K ./.sunw/pkcs11_softtoken/private
4.0K ./.sunw/pkcs11_softtoken/public
16K ./.sunw/pkcs11_softtoken
20K ./.sunw
4.0K ./a/b/c
8.0K ./a/b
12K ./a
276K .
8、echo
回显指定的字符串。
-bash-3.00$ echo "I am plusboy"
I am plusboy
9、pwd
pwd (print working directory) 输出当前工作目录。
10、cat
输出文件的内容,也可以用于创建一个文件。
-bash-3.00$ cat > test_file
afasdf
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h hard_link kstat kstat.c sys_link t1000 test_file
-bash-3.00$ cat test_file
afasdf
-bash-3.00$
11、wc, head, tail
wc (word count) 统计给定文件的行数/单词数/字节数。
-bash-3.00$ wc ethnet.c
1098 3102 27030 ethnet.c
head 输出文件的头部内容
tail 输出文件的尾部内容
head和tail命令可以通过“-n”参数指定要输出多少行。
-bash-3.00$ head -n 2 ethnet.c
/**************************************************************************
File: eth_net.c
tail命令还可以通过“-f”参数来观察文件的尾部变化,这个命令对查看日志文件的变化特别有用。
head和tail命令相结合还可以查看文件中从第m行到第n行的内容。例如
-bash-3.00$ head -n 100 ethnet.c | tail -n 20
break;
}
}
if (inst_len > 0) {
*instance = (char*) malloc(inst_len + 1);
strncpy(*instance, ch + 1, inst_len);
*((*instance) + inst_len) = '/0';
} else {
*instance = NULL;
}
/* Get other string */
module_len = if_len - inst_len;
if (module_len > 0) {
*module = (char*)malloc(module_len + 1);
strncpy(*module, if_name, module_len);
*((*module) + module_len) = '/0';
} else {
*module = NULL;
查看文件中第80行到第100行的内容。
12、more, less
more和less可以查看整个文件的内容,不同的是more命令一次显示完整个文件的内容,如果文件比较大,前面的内容可能就看不到了;而less可以分页显示。
13、scp
scp (ssh copy),把文件从一台主机通过ssh复制到另一台主机上。
-bash-3.00$scp file user@remote_host:/tmp
14、file
file命令查看指定文件的类型
-bash-3.00$ file ethnet.c
ethnet.c: ascii text
-bash-3.00$ file ethnet
ethnet: empty file
-bash-3.00$ file hard_link
hard_link: ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped, no debugging information available
15、tar, gzip, gunzip
tar 把若干文件打包成一个文件
gzip 压缩文件
gunzip 解压文件
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h hard_link kstat kstat.c sys_link t1000 test_file
-bash-3.00$ tar cvf test.tar *.c
a di.c 1K
a ethnet.c 27K
a kstat.c 1K
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h hard_link kstat kstat.c sys_link t1000 test.tar test_file
-bash-3.00$ gzip test.tar
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h hard_link kstat kstat.c sys_link t1000 test.tar.gz test_file
-bash-3.00$ gunzip test.tar.gz
-bash-3.00$ ls
a di di.c ethnet ethnet.c ethnet.h hard_link kstat kstat.c sys_link t1000 test.tar test_file
感谢sun unix-center,本文提供的例子都是在上面完成的