Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

Rename all my files within the folder with the prefix "Unix_"

Suppose the folder has two files

a.txt

b.pdf

then they both should be renamed from the single command to

Unix_a.txt

Unix_b.pdf

1 Answer

0 votes
by (36.8k points)
edited by

If your filenames contain no whitespace and you don't have any subdirectories, you can use this simple for loop:

$ for FILENAME in *; do mv $FILENAME Unix_$FILENAME; done 

Otherwise use this convenient rename command (which is the perl script) - although it might not be available out of this box on every Unix (e.g. OS X doesn't come with the rename).

If your filenames contain whitespace it's easier to use the find, on Linux. The following should work:

$ find . -type f -name '*' -printf "echo mv '%h/%f' '%h/Unix_%f\n'" | sh

On BSD systems, there is no -printf option, unfortunately. But GNU findutils should be installable (on e.g. Mac OS X with brew install findutils).

$ gfind . -type f -name '*' -printf "mv \"%h/%f\" \"%h/Unix_%f\"\n" | sh

Want to be a Linux expert? Come and join this linux course

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 5, 2020 in Linux by blackindya (18.4k points)

Browse Categories

...