KItis
KItis

Reputation: 5646

What does this bash script means

I've found the following line of code in a script. Could someone explain me what does this following line of code means?

Basically, the purpose of this line is find a set of files to archive. Since I am not familiar with bash scripts, it is difficult for me to understand this line of code.

_filelist=`cd ${_path}; find . -type f -mtime ${ARCHIVE_DELAY} -name "${_filename}" -not -name "${_ignore_filename}" -not -name "${_ignore_filename2}"`

Upvotes: 1

Views: 263

Answers (3)

user unknown
user unknown

Reputation: 36269

_filelist=`somecode`

makes the variable _filelist contain the output of the command somecode.

Somecode, in this case, is mostly a find command, which searches recursively for files.

find . -type f -mtime ${ARCHIVE_DELAY} -name "${_filename}" -not -name "${_ignore_filename}" -not -name "${_ignore_filename2}"

find .

searches the current dir, but this was just before changed to be _path.

-type f

only searches in ordinary files (not dirs, sockets, ...)

-mtime

specifies the modification time of that files, to be the same as ${ARCHIVE_DELAY}

-name explains

itself, has to be "${_filename}"

-not name

explains itself too, I guess.

So the whole part sets the variable filelist to files, found by some criterias: name, age, and type.

Upvotes: 1

dogbane
dogbane

Reputation: 274828

Let's break it down:

cd ${_path} : changes to the directory stored in the ${_path} variable

find is used to find files based on the following criteria:

  • . : look in the current directory and recurse through all sub-directories
  • -type f: look for regular files only (not directories)
  • -mtime ${ARCHIVE_DELAY} : look for files last modified ${ARCHIVE_DELAY}*24 hours ago
  • -name "${_filename}": look for files which have name matching ${_filename}
  • -not -name "${_ignore_filename}" : do not find files which have name matching ${_ignore_filename}
  • -not -name "${_ignore_filename2}" : do not find files which have name matching ${_ignore_filename2}

All the files found are stored in a variable called _filelist.

Upvotes: 4

Paolo Tedesco
Paolo Tedesco

Reputation: 57262

The backtick (`) symbol assigns to the variable the output of the command.
Your script is assigning to $_filelist what you get by:

  1. Changing directory to $_path
  2. Finding in the current directory (.) files (-type f) where
    1. Name is $_filename (a pattern, I suppose)
    2. Name is not $_ignore_filename or $_ignore_filename2

I think you could as well change that to find ${_path} ... without the cd, but please try it out.

Upvotes: 4

Related Questions