Скрипты линукс проверка файлов

Как проверить, существует ли файл или каталог в Bash

Часто при написании сценариев оболочки вы можете оказаться в ситуации, когда вам нужно выполнить действие в зависимости от того, существует файл или нет.

В Bash вы можете использовать команду test, чтобы проверить, существует ли файл, и определить тип файла.

Команда test принимает одну из следующих синтаксических форм:

test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]] 

Если вы хотите, чтобы ваш сценарий был переносимым, вам следует предпочесть старую команду test [ , которая доступна во всех оболочках POSIX. Новая обновленная версия тестовой команды [[ (двойные скобки) поддерживается в большинстве современных систем, использующих Bash, Zsh и Ksh в качестве оболочки по умолчанию.

Проверьте, существует ли файл

При проверке существования файла наиболее часто используются операторы FILE -e и -f . Первый проверит, существует ли файл независимо от типа, а второй вернет истину, только если ФАЙЛ является обычным файлом (а не каталогом или устройством).

Наиболее удобочитаемый вариант при проверке существования файла — использование команды test в сочетании с оператором if . Любой из приведенных ниже фрагментов проверит, существует ли файл /etc/resolv.conf :

FILE=/etc/resolv.conf if test -f "$FILE"; then echo "$FILE exists." fi 
FILE=/etc/resolv.conf if [ -f "$FILE" ]; then echo "$FILE exists." fi 
FILE=/etc/resolv.conf if [[ -f "$FILE" ]]; then echo "$FILE exists." fi 

Если вы хотите выполнить другое действие в зависимости от того, существует файл или нет, просто используйте конструкцию if / then:

FILE=/etc/resolv.conf if [ -f "$FILE" ]; then echo "$FILE exists." else echo "$FILE does not exist." fi 

Всегда используйте двойные кавычки, чтобы избежать проблем при работе с файлами, в именах которых есть пробелы.

Вы также можете использовать команду test без оператора if. Команда после оператора && будет выполнена только в том случае, если статус выхода тестовой команды — истина,

test -f /etc/resolv.conf && echo "$FILE exists." 
[ -f /etc/resolv.conf ] && echo "$FILE exists." 
[[ -f /etc/resolv.conf ]] && echo "$FILE exists." 

Если вы хотите запустить серию команд после оператора && просто заключите команды в фигурные скобки, разделенные ; или && :

[ -f /etc/resolv.conf ] &&  echo "$FILE exist."; cp "$FILE" /tmp/; > 

Напротив && , оператор после || Оператор будет выполняться только в том случае, если статус выхода тестовой команды false .

[ -f /etc/resolv.conf ] && echo "$FILE exist." || echo "$FILE does not exist." 

Проверить, существует ли каталог

Операторы -d позволяют вам проверить, является ли файл каталогом или нет.

Например, чтобы проверить, существует ли каталог /etc/docker вы должны использовать:

FILE=/etc/docker if [ -d "$FILE" ]; then echo "$FILE is a directory." fi 
[ -d /etc/docker ] && echo "$FILE is a directory." 

Вы также можете использовать двойные скобки [[ вместо одинарной [ .

Проверьте, не существует ли файла

Как и во многих других языках, тестовое выражение может быть отменено с помощью ! (восклицательный знак) оператор логического НЕ:

FILE=/etc/docker if [ ! -f "$FILE" ]; then echo "$FILE does not exist." fi 
[ ! -f /etc/docker ] && echo "$FILE does not exist." 

Проверьте, существует ли несколько файлов

Вместо использования сложных вложенных конструкций if / else вы можете использовать -a (или && с [[ ), чтобы проверить, существует ли несколько файлов:

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then echo "Both files exist." fi 
if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then echo "Both files exist." fi 

Эквивалентные варианты без использования оператора IF:

[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist." 
[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist." 

Операторы проверки файлов

Команда test включает в себя следующие операторы FILE, которые позволяют проверять файлы определенных типов:

  • -b FILE — Истина, если ФАЙЛ существует и является специальным блочным файлом.
  • -c FILE — Истина, если ФАЙЛ существует и является файлом специальных символов.
  • -d FILE — Истина, если ФАЙЛ существует и является каталогом.
  • -e FILE — Истина, если ФАЙЛ существует и является файлом, независимо от типа (узел, каталог, сокет и т. д.).
  • -f FILE — Истина, если ФАЙЛ существует и является обычным файлом (не каталогом или устройством).
  • -G FILE — Истина, если ФАЙЛ существует и имеет ту же группу, что и пользователь, выполняющий команду.
  • -h FILE — Истина, если ФАЙЛ существует и является символической ссылкой.
  • -g FILE — Истина, если ФАЙЛ существует и для него установлен флаг set-group-id ( sgid ).
  • -k FILE — Истина, если ФАЙЛ существует и для него установлен флаг липкого бита.
  • -L FILE — Истина, если ФАЙЛ существует и является символической ссылкой.
  • -O FILE — Истина, если ФАЙЛ существует и принадлежит пользователю, выполняющему команду.
  • -p FILE — Истина, если ФАЙЛ существует и является каналом.
  • -r FILE — Истинно, если ФАЙЛ существует и доступен для чтения.
  • -S FILE — Истина, если ФАЙЛ существует и является сокетом.
  • -s FILE — Истина, если ФАЙЛ существует и имеет ненулевой размер.
  • -u FILE — Истинно, если ФАЙЛ существует и установлен флаг set-user-id ( suid ).
  • -w FILE — Истина, если ФАЙЛ существует и доступен для записи.
  • -x FILE — Истина, если ФАЙЛ существует и является исполняемым.

Выводы

В этом руководстве мы показали вам, как проверить, существует ли файл или каталог в Bash.

Если у вас есть какие-либо вопросы или отзывы, не стесняйтесь оставлять комментарии.

Источник

How to check if a file exists in a shell script

I’d like to write a shell script which checks if a certain file, archived_sensor_data.json , exists, and if so, deletes it. Following http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html, I’ve tried the following:

[-e archived_sensor_data.json] && rm archived_sensor_data.json 

when I try to run the resulting test_controller script using the ./test_controller command. What is wrong with the code?

You must set one or more whitespace between opening square bracket «[» and option «-e» same as between filename and closing square bracket «]»

7 Answers 7

You’re missing a required space between the bracket and -e :

#!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi 

I finally added two blank spaces, one after the opening square bracket and one before the closing one: [ -e archived_sensor_data.json ] && rm archived_sensor_data.json . The script seems to work now.

The main difference here is the fact that you are using «bash» scripting instead of «shell» scripting. Notice that the first line that you have added was #!/bin/bash, so you are telling the machine to use «bash» instead of sh. Because sh doesn’t recognize that argument «-e»

Here is an alternative method using ls :

(ls x.txt && echo yes) || echo no 

If you want to hide any output from ls so you only see yes or no, redirect stdout and stderr to /dev/null :

(ls x.txt >> /dev/null 2>&1 && echo yes) || echo no 

This code means: «if ls is successful, there is such file, otherwise, there is none». If ls failed, it does not mean that file is missing. It might be some other error. For example, create file in directory owned by root and try to do ls under regular user. It will fail with Permission denied , which is not equivalent that file does not exist.

The backdrop to my solution recommendation is the story of a friend who, well into the second week of his first job, wiped half a build-server clean. So the basic task is to figure out if a file exists, and if so, let’s delete it. But there are a few treacherous rapids on this river:

  • Everything is a file.
  • Scripts have real power only if they solve general tasks
  • To be general, we use variables
  • We often use -f force in scripts to avoid manual intervention
  • And also love -r recursive to make sure we create, copy and destroy in a timely fashion.

Consider the following scenario:

We have the file we want to delete: filesexists.json

This filename is stored in a variable

:~/Documents/thisfolderexists filevariable="filesexists.json" 

We also hava a path variable to make things really flexible

:~/Documents/thisfolderexists pathtofile=".." :~/Documents/thisfolderexists ls $pathtofile filesexists.json history20170728 SE-Data-API.pem thisfolderexists 

So let’s see if -e does what it is supposed to. Does the files exist?

:~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0 

However, what would happen, if the file variable got accidentally be evaluated to nuffin’

:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0 

What? It is supposed to return with an error. And this is the beginning of the story how that entire folder got deleted by accident

An alternative could be to test specifically for what we understand to be a ‘file’

:~/Documents/thisfolderexists filevariable="filesexists.json" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 0 
:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 1 

So this is not a file and maybe, we do not want to delete that entire directory

man test has the following to say:

-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file . -h FILE FILE exists and is a symbolic link (same as -L) 

Источник

Читайте также:  Замена rufus на linux
Оцените статью
Adblock
detector