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



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

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?


  • 3
    Change "$WATCHED/$new" to "$new"
    – jordanm
    Commented Dec 16, 2020 at 17:05
  • Yes that worked! I knew it had to be simple. Thanks!
    – S. Ray
    Commented Dec 16, 2020 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
    Commented Dec 16, 2020 at 18:30

1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .