user2141414
user2141414

Reputation: 101

Unix - Shell script to find a file from any directory and move it

i'm currently working through an exercise book and I have to create a shell script that will find a file from any directory and move it.

Though I am having difficulties as the file could be in any directory (so I do not have a path to find it). I have used the find option with the -print flag tho what would be the next step to move it using mv command?

My code so far reads in a variable, detects if a file has been entered, if it is a file or a directory, or if it exists.

The next stage as mentioned above is to find the file and then move it into a "test" file.

If anyone has any recommendations it would be greatly appreciated.

#!/bin/bash

bin="deleted"

if [ !  -e bin ] ; then
    mkdir $bin
fi

file=$1

#error to display that no file has been entered
if [[ ! $file ]]; then
    echo "no file has been entered"
fi

#file does not exist, display error message
if [[ !  -f $file ]]; then
    echo "$file does not exsist!"
fi

#check to see if the input is a directory
if [[ -d $file ]]; then
    echo "$file is a directory!"

if [[ -e $file ]]; then *** move to test folder
****This is where I am having the problems

Upvotes: 1

Views: 18701

Answers (1)

necromancer
necromancer

Reputation: 24641

find / -type f -name FILENAME | xargs -I foobar echo mv foobar /tmp (remove echo to make the command actually work .. i put it there just to save yourself from accidentally moving files just to try out the command)

Note that -I foobar means that in mv foobar /tmp replace the foobar string with full path of the file found.

for example, try: find / -type f -name FILENAME | xargs -I foobar foobar is a cool file

Upvotes: 3

Related Questions