Skip to main content
added 41 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
$ find . -type f -name "file*.txt" -execdir mv {} new_{} \;

This will find all regular files in the current directory (or below) that have names that matches the pattern file*.txt, and rename these by adding the prefix new_ to their names.

RequiresThis requires a find that understands -execdir (most modern find implementations do). ThisThe -execdir option works like -exec but executes the utility (mv) in the directory of the found thing. Also, {} will contain the basename of the found thing.

To limit to the current directory only, add -maxdepth 1 somewhere before -execdir.


bash-4.4$ mkdir dir{1..10}
bash-4.4$ touch dir{1..10}/file{1..10}.txt

bash-4.4$ ls
dir1  dir10 dir2  dir3  dir4  dir5  dir6  dir7  dir8  dir9

bash-4.4$ ls dir5
file1.txt   file2.txt   file4.txt   file6.txt   file8.txt
file10.txt  file3.txt   file5.txt   file7.txt   file9.txt

bash-4.4$ find . -name "file*.txt" -execdir mv {} new_{} \;

bash-4.4$ ls dir5
new_file1.txt    new_file2.txt    new_file4.txt    new_file6.txt    new_file8.txt
new_file10.txt   new_file3.txt    new_file5.txt    new_file7.txt    new_file9.txt
$ find . -type f -name "file*.txt" -execdir mv {} new_{} \;

This will find all regular files in the current directory (or below) that have names that matches the pattern file*.txt, and rename these by adding the prefix new_ to their names.

Requires a find that understands -execdir (most modern find implementations do). This works like -exec but executes in the directory of the found thing. Also, {} will contain the basename of the found thing.

To limit to the current directory only, add -maxdepth 1 somewhere before -execdir.

$ find . -type f -name "file*.txt" -execdir mv {} new_{} \;

This will find all regular files in the current directory (or below) that have names that matches the pattern file*.txt, and rename these by adding the prefix new_ to their names.

This requires a find that understands -execdir (most modern find implementations do). The -execdir option works like -exec but executes the utility (mv) in the directory of the found thing. Also, {} will contain the basename of the found thing.

To limit to the current directory only, add -maxdepth 1 somewhere before -execdir.


bash-4.4$ mkdir dir{1..10}
bash-4.4$ touch dir{1..10}/file{1..10}.txt

bash-4.4$ ls
dir1  dir10 dir2  dir3  dir4  dir5  dir6  dir7  dir8  dir9

bash-4.4$ ls dir5
file1.txt   file2.txt   file4.txt   file6.txt   file8.txt
file10.txt  file3.txt   file5.txt   file7.txt   file9.txt

bash-4.4$ find . -name "file*.txt" -execdir mv {} new_{} \;

bash-4.4$ ls dir5
new_file1.txt    new_file2.txt    new_file4.txt    new_file6.txt    new_file8.txt
new_file10.txt   new_file3.txt    new_file5.txt    new_file7.txt    new_file9.txt
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

$ find . -type f -name "file*.txt" -execdir mv {} new_{} \;

This will find all regular files in the current directory (or below) that have names that matches the pattern file*.txt, and rename these by adding the prefix new_ to their names.

Requires a find that understands -execdir (most modern find implementations do). This works like -exec but executes in the directory of the found thing. Also, {} will contain the basename of the found thing.

To limit to the current directory only, add -maxdepth 1 somewhere before -execdir.