Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check whether a file exists in a shell script

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Shell script how to check the existence of files, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. # ll-h upload.zip

-rw-r--r-- 1 root root 3.3M 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 used% mount point

/ dev/hda3 9.5G 5.7G 3.4G 64% /

The following script will combine 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 give executable permissions to the script!

Chomd upright x wenjian.sh

The result of executing the script is as follows:

# / jiaoben/wenjian.sh upload.zip

Properties for upload.zip:

Size is 3.3M

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 more convenient than typing commands one by one to check the file information.

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 above-b,-c and-f represent bytes, characters and fields (that is, byte, character, field)

List denotes the operating range of-b,-c,-f, and-n often indicates specific numbers.

File represents, of course, the name of the text file to be operated on.

Delim (delimiter) indicates the delimiter, which is TAB by default

-s does not include lines that do not contain delimiters (this helps to remove comments and headers)

In the above three ways, it means to extract bytes (- b), or characters (- c), or fields (- f) from the specified range.

The representation of the range:

M

Only item M

M-

From item M to the end of the line

Mmurn

From item M to item N (including N)

-N

From the beginning of a line to item N (including N)

-

All items from the beginning to the end of a line

Example:

# cat example

Test2

This is test1

# cut-C1-6 example # # print starts with the first 6 characters

Test2

This i

-c mmurn indicates that the m-th to nth characters of each line are displayed. For example:

-file-

Wokao 84 25000

-file-

# cut-c 1-5 file 10-25

Wokao 25000

-f mmurn indicates that columns m to n are displayed (separated by tab). For example:

-file-

Wokao 84 25000

-file-

# cut-f 1pr 3 file

Wokao 25000

We often encounter certain fields of a file that need to take out sub-fields. For example, / etc/password separates the fields by ":". This can be done through the cut command. For example,

If we want to save the system account name to a specific file, we can:

Cut-d ":"-f 1 / etc/passwd > / tmp/users

-d is used to define the delimiter. The default is the tab key, and-f indicates which field needs to be obtained.

Such as:

Use | Separation

Cut-d' |'- f2 1.test > 2.test

Using: separating

Cut-danzhu 1.test'- f2 1.test > 2.test

Either single or double quotation marks can be used here.

After reading the above, have you mastered how to check whether the file exists in the shell script? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report