Assume we want to move specific files from folder S to folder D, namely those that do not contain the string jamero and are older than one hour.

find S -type f -mmin +60 | xargs -i grep -v -l jamero | xargs -i mv '{}' D

There are different opinions on the aesthetic value of this solution.  However, it is undeniably inaccessible for many people that have demand to such helper but have little bash-experience. As beginners, some knowledge of the find command presumed, we would rather try something like this:

OLD_FILES=$(find S -type f -mmin +60)

for FILE in $OLD_FILES; do
    if [[ $(grep jamero $FILE) == "" ]]; then
        mv $FILE D

Unfortunately, we now cannot cope with whitespaces in paths. We want OLD_FILES to be a list the entries of wich are paths. But it is a string and the for-structure splits it by whitespace. One way to deal with it is to explicitly state that splitting should be done at newlines.

Here, this does not help, because $(..) replace the newlines by spaces. Although the task is clear and simple, we get lost in tinkering. Note that the first solution might be shiny but it misses empty files.

1 comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: