Shell file test

Source: Internet
Author: User

File Type:

-F: determines whether the file is a common file.
-D: determines whether the directory is used.

-B: determines whether a device file is a block.
-C: determines whether a device file is a dual-character device file.
-S: Determine whether the socket file is used
-P: Determine whether the MPs queue file is used.
-H: indicates that all values are symbolic links.

-L: The file exists and has symbolic links.
File Size existence:
-E: determines whether a file or directory exists.
-S: the file or directory exists and the size is greater than 0.

File read/write features
-R: determines whether the object has the permission to read.
-W: determines whether a file has the writable permission.
-X: determines whether a file has executable permissions.

-G: determines whether the file has a sgid bit.
-U: determines whether the file has a suid.

-K: determines whether a sticky bit is set. After the sticky bit is set, the file will be written to the cache.

File modification time:
File1-nt file2: Determine if file1 is newer than file2
File1-ot file2: Determine if file1 is older than file2

Instance 1:

Print the directory recursively

#!/bin/bash function readir(){for file in `ls $1`do   if [ -d $1"/"$file ]   then       readir $1"/"$file;   else      echo $1"/"$file;  fidone }readir /tmp
Example 2:

Check whether the files or directories in a directory are readable and writable and executable.

#!/bin/bash function filetest(){for file in `ls $1`do    if [ -r $file -a -w $file -a -x $file ]    then           echo $file;    fidone}  filetest /tmp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.