Need your help on a command that run on a path1 to rename and move files from path2 to path3


path1 = /data/run/
path2 = /data/output/
path3 = /data/archive/

path 2 contains few files like 'one.txt', 'two.txt' etc...

I want to run a command in path1 which can rename the files 'one.txt' to 'archive_one.txt' and move them to path3

I tried the below command from '/data/run' , but it is taking the whole path as file name and throwing error.

$for FILENAME in /data/output/*.txt; do mv $FILENAME /data/archive/archive_$FILENAME; done

How can I do that. Thanks.

1 Answer 1


what you want to do is

for FILENAME in /data/output/*.txt; 
   mv "$FILENAME" "/data/archive/archive_$(basename  "$FILENAME")" ; 

this can be one lined of course.


  • basename "$FILENAME" extract last part of filename
  • basename "$FILENAME" .txt would do the ame, striping .txt part.

and when posting here thou shall quote filename always.

  • Thanks, this answers my question. I got a small problem, sometime the file names do not have any extension and that path contains some sub directories too. In case if I put . then the entire files and sub-directories are moved. Is there a way to restrict to move only the files and not directories? Commented Jul 31, 2020 at 5:57

You must log in to answer this question.

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