Volodymyr
Volodymyr

Reputation: 1442

how to get arguments of find command and print them out?

I want to list only folders which were not modified during last 30 days and print them.

  workspace_ts="/home/user/workspace"
    if [[ -n $(find $workspace_ts -mindepth 1 -maxdepth 1 -type d -mtime -30) ]];then
        echo -e "\t- It seems that [folder_name] wasn't modified during last 30 days"
    fi

how can I get arguments of find command and print them out?

Upvotes: 0

Views: 379

Answers (3)

kenorb
kenorb

Reputation: 166765

You don't need loop for that, as you can execute command directly in find using -exec/-execdir parameter where your folder name is in {}, e.g.:

find /home/user/workspace -mindepth 1 -maxdepth 1 -type d -mtime +30 -execdir echo "It seems that {} wasn't modified during last 30 days" ';'

which will print you name of folders. In case you need absolute paths, then change -execdir to -exec.

Above syntax will work for both BSD & GNU find, however with GNU the folder name will be prefixed with ./. See: Why the dot in find commands? at Unix SE.

To follow the symbolic links, consider adding -L.

If you're interested in portability, check: How to force GNU find to follow POSIX standard?

Upvotes: 1

123
123

Reputation: 11226

If all you're doing is printing a message you can use the printf option as it is the most efficient and probably easiest way.

find /home/user/workspace -mindepth 1 -maxdepth 1 -type d -mtime +30 -printf "\t- It seems that %p wasn't modified during last 30 day\n"

%p is the name of the file(with full path from directory find is run in)

%f if you just want the basename.

There are a lot of other useful things you can display with it as well which can be found under the printf section of the man page

Upvotes: 2

Leh Valensa
Leh Valensa

Reputation: 94

You can do in in for loop:

workspace_ts="/home/user/workspace";
for folder_name in `find $workspace_ts -mindepth 1 -maxdepth 1 -type d -mtime +30 -print`; do
  echo -e "\t- It seems that [$folder_name] wasn't modified during last 30 days";
done;

Upvotes: 0

Related Questions