3

I have 100 files. I want to add a text 'new' before all the filenames.

Please help me. Thanks in advance.

Example:

file1.txt  --->  new_file1.txt
.
.
.
.
file100.txt  ---> new_file100.txt

Please provide a solution to rename multiple files. Here is what I have tried. But this is not a better solution.

 
bala@SMS:~/test1$ ls -l
total 0
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file1.txt
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file2.txt
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file3.txt
bala@SMS:~/test1$ mv file1.txt new_file1.txt
bala@SMS:~/test1$ mv file2.txt new_file2.txt
bala@SMS:~/test1$ mv file3.txt new_file3.txt
bala@SMS:~/test1$ ls
new_file1.txt  new_file2.txt  new_file3.txt
bala@SMS:~/test1$ 
3

You should use loop to change filename of multiple files:

for file in *
do
    mv -v ${file} new_${file}
done

The same code in one line:

for file in *; do mv -v ${file} new_${file}; done
  • I would change that to for file in "/path/to/files/file*.txt" just to avoid this running a muck in the wrong directory. – Jesse_b Aug 31 '17 at 14:19
  • ` bala@SMS:~/test1$ ls -l total 4 -rw-rw-r-- 1 bala bala 0 Aug 31 19:47 file1.txt -rw-rw-r-- 1 bala bala 0 Aug 31 19:47 file2.txt -rw-rw-r-- 1 bala bala 0 Aug 31 19:47 file3.txt -rw-rw-r-- 1 bala bala 49 Aug 31 19:41 new_test.sh bala@SMS:~/test1$ sh new_test.sh ‘file1.txt’ -> ‘new_file1.txt’ ‘file2.txt’ -> ‘new_file2.txt’ ‘file3.txt’ -> ‘new_file3.txt’ ‘new_test.sh’ -> ‘new_new_test.sh’ bala@SMS:~/test1$ ls new_file1.txt new_file2.txt new_file3.txt new_new_test.sh bala@SMS:~/test1$ ` – Dipankar Nalui Aug 31 '17 at 14:22
  • 2
    It's hard to read what you pasted there but I suppose it's working – mrc02_kr Aug 31 '17 at 14:26
2
$ 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
0

You could use rename command; check if you have installed on your system or install it with your OS package manager

[root@archy pippo]# rename -h

Usage:
 rename [options] <expression> <replacement> <file>...

Rename files.

Options:
 -v, --verbose    explain what is being done
 -s, --symlink    act on the target of symlinks

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see rename(1).

in your case, you have to do this simple substitution

[root@archy pippo]# ls -lrta
total 8
drwxr-x--- 6 root root 4096 Aug 31 14:12 ..
-rw-r--r-- 1 root root    0 Aug 31 14:12 file1.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file2.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file3.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file4.txt
drwxr-xr-x 2 root root 4096 Aug 31 14:24 .
[root@archy pippo]# rename -v file new_file *.txt
`file1.txt' -> `new_file1.txt'
`file2.txt' -> `new_file2.txt'
`file3.txt' -> `new_file3.txt'
`file4.txt' -> `new_file4.txt'
[root@archy pippo]# ls -lrta
total 8
drwxr-x--- 6 root root 4096 Aug 31 14:12 ..
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file1.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file2.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file3.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file4.txt
drwxr-xr-x 2 root root 4096 Aug 31 14:27 .
[root@archy pippo]#

et voilà :)

  • sorry.. but it did not work for me. i could see this. --> Bareword "file" not allowed while "strict subs" in use at (eval 1) line 1. – Dipankar Nalui Aug 31 '17 at 14:55
  • I believe some systems (Debian included?) have a different (perl-related?) tool available as rename, which does not accept the syntax in @WesT_Tale's answer. – njsg Aug 31 '17 at 16:23
  • An answer at StackOverflow has an example of the invocation for that other rename tool. – njsg Aug 31 '17 at 16:28
0

Use mv with Brace Expansion within a loop.

for N in {1..100}; do
     mv {,new_}file$N.txt
done

Before running the mv command, shell will expand it to the command like this for each file where $N will substitute with corresponding sequence number.

mv file$N.txt new_file$N.txt

so this appending a prefix to the fileName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.