0

How do I use just the filename to rename files use this script syntax?

#!/bin/bash

WATCHED=/mnt/Watched
DESTINATION=/mnt/Destination

inotifywait -re CLOSE_WRITE,CREATE,MOVED_TO --format $'%e\t%w%f' -m $WATCHED |
    while IFS=$'\t' read -r events new
    do
        cp -r "$WATCHED/$new" "$DESTINATION/"
    done

When I run this I get this result

cp: cannot stat '/mnt/Watched//mnt/Watched/newfile.mkv': No such file or directory

I know that if the format was setup like this --format %f it would work fine but I cannot set it up that way as I need the events and full path for other parts of my script that I left out. So how do I pull out and assign just the filename for my cp command?

Thanks

3
  • 3
    Change "$WATCHED/$new" to "$new"
    – jordanm
    Dec 16 '20 at 17:05
  • Yes that worked! I knew it had to be simple. Thanks!
    – S. Ray
    Dec 16 '20 at 18:28
  • If you have a solution, then post it as an answer explaining what the issue was and how the solution solves the issue. If the solution does not require an answer, then delete your question, as it couldn't have been important. I have rolled back your tagging in the title.
    – Kusalananda
    Dec 16 '20 at 18:30
0

I changed "$WATCHED/$new" to "$new" per jordanm's suggestion and it now works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.