- S_ISREG 普通文件
- S_ISDIR 目录文件
- S_ISCHR 字符特殊文件
- S_ISBLK 块设备文件
- S_ISFIFO 管道文件
- S_ISLNK 符号链接文件
- S_ISSOCK 套接字文件
- echo xxx>testfile
点击(此处)折叠或打开
- #include
- struct stat statbuf={0};
- if(stat(testfile,&statbuf)!=0)
- {
- printf("stat failed\n");
- return -1;
- }
- if(S_ISREG(statbuf.st_mode))
- {
- printf("testfile is REGULAR file\n" );
- }
- [beanl@localhost fs_shell]$ echo xxx>testfile
- [beanl@localhost fs_shell]$ mkfifo fifo_test
- [beanl@localhost fs_shell]$ mkdir testdir
- [beanl@localhost fs_shell]$ ln testfile testfile_hdlink
- [beanl@localhost fs_shell]$ ln -s testfile testfile_sflink
- [beanl@localhost fs_shell]$ ll -ai
- total 24
- 26187022 drwxrwxr-x 3 beanl beanl 4096 Jun 23 12:33 .
- 26187016 drwxrwxr-x 4 beanl beanl 4096 Jun 23 10:59 ..
- 26187064 prw-rw-r-- 1 beanl beanl 0 Jun 23 12:32 fifo_test
- 26187065 drwxrwxr-x 2 beanl beanl 4096 Jun 23 12:33 testdir
- 26187063 -rw-rw-r-- 2 beanl beanl 4 Jun 23 12:32 testfile
- 26187063 -rw-rw-r-- 2 beanl beanl 4 Jun 23 12:32 testfile_hdlink
- 26187066 lrwxrwxrwx 1 beanl beanl 8 Jun 23 12:33 testfile_sflink -> testfile
- 26187073 -rwxrwxr-x 1 beanl beanl 939 Jun 23 12:08 test.sh
我们看一下:
- [beanl@localhost fs_shell]$ cat testfile
- xxx
- [beanl@localhost fs_shell]$ cat testfile_hdlink
- xxx
- [beanl@localhost fs_shell]$ echo zzzz>testfile_hdlink
- [beanl@localhost fs_shell]$ cat testfile_hdlink
- zzzz
- [beanl@localhost fs_shell]$ cat testfile
- zzzz
- [beanl@localhost fs_shell]$
- [beanl@localhost fs_shell]$ ln -s /home/beanl/code/LINUX/fs_shell/testfile testfile_sflink_2
- [beanl@localhost fs_shell]$ ll
- total 16
- prw-rw-r-- 1 beanl beanl 0 Jun 23 12:32 fifo_test
- drwxrwxr-x 2 beanl beanl 4096 Jun 23 12:33 testdir
- -rw-rw-r-- 2 beanl beanl 5 Jun 23 12:44 testfile
- -rw-rw-r-- 2 beanl beanl 5 Jun 23 12:44 testfile_hdlink
- lrwxrwxrwx 1 beanl beanl 8 Jun 23 12:33 testfile_sflink -> testfile
- lrwxrwxrwx 1 beanl beanl 40 Jun 23 12:48 testfile_sflink_2 -> /home/beanl/code/LINUX/fs_shell/testfile
- -rwxrwxr-x 1 beanl beanl 939 Jun 23 12:08 test.sh
- [beanl@localhost fs_shell]$ ln -s testfile testfile_sflink
点击(此处)折叠或打开
- [beanl@localhost fs_shell]$ ln -s /home/beanl/code/LINUX/fs_shell/testfile testfile_sflink_2
- [beanl@localhost fs_shell]$ readlink -f testfile_sflink
- /home/beanl/code/LINUX/fs_shell/testfile
- #!/bin/sh
- if [ -e filenotexist ]
- then
- echo "filenotexist existed"
- else
- echo "filenotexist not existed"
- fi
- if [ -d testdir ]
- then
- echo "testdir is a dir"
- fi
- if [ -f testfile ]
- then
- echo "testfile is a regular file"
- else
- echo "testfile is not a regular file"
- fi
- if [ -h testfile_sflink ]
- then
- REAL_PATH=`readlink -f testfile_sflink`
- echo "testfile_sflink is a soft link point to " $REAL_PATH
- else
- echo "testfile_sflink is not a soft link"
- fi
- if [ -p fifo_test ]
- then
- echo "fifo_test is a pipe file"
- else
- echo "fifo_test is not a pipe file"
- fi
- if [ -b /dev/sdb1 ]
- then
- echo "/dev/sdb1 is block device file"
- else
- echo "/dev/sdb1 is not block device file"
- fi
- if [ -c /dev/sdb1 ]
- then
- echo "/dev/sdb1 is charactor device file"
- else
- echo "/dev/sdb1 is not charactor device file"
- fi
- if [ -c /dev/tty0 ]
- then
- echo "/dev/tty0 is character device file"
- else
- echo "/dev/tty0 is not character device file"
- fi
- [beanl@localhost fs_shell]$ ./test.sh
- filenotexist not existed
- testdir is a dir
- testfile is a regular file
- testfile_sflink is a soft link point to /home/beanl/code/LINUX/fs_shell/testfile
- fifo_test is a pipe file
- /dev/sdb1 is block device file
- /dev/sdb1 is not charactor device file
- /dev/tty0 is character device file
- [beanl@localhost fs_shell]$