Shell script -- check whether the file exists

Source: Internet
Author: User

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] [file...]

Cut-c list [file...]

Cut-f list [-d delim] [-s] [file...]

The preceding-B,-c, and-f indicate byte, character, and field, that is, byte, character, and field, respectively );

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 English: delimiter) indicates the delimiter, which is a TAB by default;

-S indicates that rows without delimiters are not included. This helps remove comments and titles)

In the preceding three methods, bytes-B),-c), or field-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.

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.