From: http:// OS .51cto.com/art/200912/165922.htm
Write a script to check whether a file exists. If so, output its details. If not, the system prompts that the output file does not exist. Before providing this script, let's take a look at the following commands: file upload.zip.
1. # ll-H upload.zip
-RW-r -- 1 Root 3.3 m 06-28 23:21 upload.zip
2. # file upload.zip
Upload.zip: ZIP archive data, at least V1.0 to extract
3. # ls-I upload.zip
1427041 upload.zip
4. # DF-H upload.zip
File System capacity used available % mount point
/Dev/hda3 9.5g 5.7g 3.4g 64%/
The following script combines these commands to display the details of a file.
#! /Bin/bash
# This script gives information about a file.
Filename = "$1"
Echo "properties for $ filename :"
If [-F $ filename]; then
Echo "size is $ (LS-lH $ filename | awk '{print $5 }')"
Echo "type is $ (File $ filename | cut-d": "-F2 -)"
Echo "inode number is $ (LS-I $ filename | cut-d" "-F1 -)"
Echo "$ (DF-h $ filename | grep-V File System | awk '{print" on ", $1 ",\
Which is mounted as the ", $6," partition ."}')"
Else
Echo "file does not exist ."
Fi
Remember to grant the script executable permission !!!!
Chomd U + x wenjian. Sh
The script execution result is as follows:
#/Jiaoben/wenjian. Sh upload.zip
Properties for upload.zip:
Size is 3.3 m
Type is ZIP archive data, at least V1.0 to extract
Inode number is 1427041
On/dev/hda3, Which is mounted as the/partition.
This is much easier than checking the file information by one command.
If you are not familiar with the cut command, you can refer to the following instructions:
-----------------------------------------------------------------------
-----------------------------------------------------------------------
The cut command can extract text columns from a text file or text stream.
Command usage:
Cut-B list [-N]
Cut-C List
Cut-F list [-D delim] [-S]
The preceding-B,-C, and-F indicate bytes, characters, and fields (namely byte, character, and field );
List indicates the range of-B,-C, and-F operations.-N often indicates a specific number;
File indicates the name of the text file to be operated;
Delim (full write: delimiter) indicates the delimiter, which is a tab by default;
-S indicates that rows without delimiters are excluded (this helps remove comments and titles)
In the preceding three methods, bytes (-B), or characters (-c), or fields (-f) are extracted from the specified range ).
Range representation:
M
Only entry m
M-
From entry m to end of the row
M-N
From item M to item n (including N)
-N
From the beginning of a row to n (including N)
-
All items from the beginning to the end of a row
Example:
# Cat Example
Test2
This is test1
# Cut-c1-6 example # the first 6 characters starting with print
Test2
This I
-C m-N indicates that the MB to n characters of each line are displayed. For example:
--------- File -----------
Wokao 84 25000
--------- File -----------
# Cut-C 1-5, 10-25 File
Wokao 25000
-F m-N indicates to display column M to column N (separated by tab ). For example:
--------- File -----------
Wokao 84 25000
--------- File -----------
# Cut-F 1, 3 File
Wokao 25000
We often encounter some specific fields in the file that need to retrieve the field, for example,/etc/password is separated. You can use the cut command. For example,
We want to save the system account name to a specific file:
Cut-d ":"-F 1/etc/passwd>/tmp/users
-D is used to define the delimiter. The default Delimiter is the tab key.-F indicates the field to be obtained.
For example:
Separated by |
Cut-d' | '-F2 1. Test> 2. Test
Separated:
Cut-d': '-F2 1. Test> 2. Test
You can use single or double quotation marks here.