Home > Net >  How to add suffix to each file in a directory
How to add suffix to each file in a directory

Time:03-27

I have four files in my directory that I need to moved to a new directory using command

mv file1.txt file2.dat file3.sh dest_dir.bak

where dest_dir.bak is the new directory.

Now I am to add suffix to all the files which are in this directory with .dat

My attempt:

for file in *; do echo mv -- "$file" "$file.dat"; done

But when I ran this command it has renamed all the files in the home directory with .dat but I need to make this changes only to the dest_dir.bak directory. How to achieve this?

CodePudding user response:

I answered below. But I prefer this method.

find . -d 1 -type f -exec mv '{}' './dest_dir.bak/{}.dat' \;

CodePudding user response:

Is this what you want to do?

for file in file*; do mv ${file} ./dest_dir.bak/${file}.dat; done     
  • Related