I have many files in a directory having extension.
.text(2)
.text(1)
I want to remove the numbers from extension and output should be like
.text
.text
Can anyone please help me with the shell script for that?i am using CentOs 6.3.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI have many files in a directory having extension.
.text(2)
.text(1)
I want to remove the numbers from extension and output should be like
.text
.text
Can anyone please help me with the shell script for that?i am using CentOs 6.3.
Start the one-liner in the folder where the files are saved or change the path for the find
command. In the following examples find .
the path is .
(dot).
Test with:
find . -type f -print0 | xargs -I{} -0 rename -v -n 's/\([0-9]+\)$//' {}
Rename with:
find . -type f -print0 | xargs -I{} -0 rename -v 's/\([0-9]+\)$//' {}
The command finds all files recursively and removes all occurrences of (<any_number>)
at the end of the file name.
Remove the $
in 's/\([0-9]+\)$//'
to remove all occurrences somewhere in the file name, eg:
find . -type f -print0 | xargs -I{} -0 rename -v 's/\([0-9]+\)//' {}
Example:
% ls -oga
-rw-rw-r-- 1 0 Jun 10 09:34 .foo(1)
-rw-rw-r-- 1 0 Jun 10 09:34 .bar(2)
% find . -type f -print0 | xargs -I{} -0 rename -v 's/\([0-9]+\)$//' {}
% ls -aog
-rw-rw-r-- 1 0 Jun 10 09:34 .foo
-rw-rw-r-- 1 0 Jun 10 09:34 .bar
s/\([0-9]+\)$//
to only remove the brackets at the end of the filename. Anyway +1 good solution
– chaos
Jun 10 '15 at 7:42
rename -v 's/\([0-9]+\)$//' {}
don't forget the backslashes.
– A.B.
Jun 10 '15 at 8:06
find . -type f -print0 | xargs -0 rename -v 's/\([0-9]+\)$//'
will do
– kos
Jun 10 '15 at 8:23
for i in *.text*; do mv "$i" "$(echo "$i" | sed 's/([0-9]\{1,\})$//')"; done
just need to change the extension (.text or any other extension) according to need.
rename
can do it very simply..
– heemayl
Jun 10 '15 at 12:07
Using python
:
#!/usr/bin/env python2
import os, re
for root, dirs, files in os.walk('/path/to/directory'):
for f in files:
oldname = os.path.join(root, f)
newname = os.path.join(root, re.search(r'(?<=/)[^/]+(?=\(\d+\)$)', oldname).group())
os.rename(oldname, newname)
Considering the first part of file names are different (as you have mentioned it already), so no chance of overwriting.
os.walk
will traverse all subdirectories under the mentioned directory
oldname
will contain the name of the file to be changed. os.path.join
will add the filename with the directory path by os.path.join
newname
will contain the name what oldname
will be change to. Here we have used the re
module to get the file name and then added the filename to the path to directory by os.path.join
os.rename
will simply rename the files accordingly.
Before :
foo
├── 1 spam.text(1)
├── 1.text(23)
└── bar
├── 1 egg.text(10)
└── 3test.text(5)
After :
foo
├── 1 spam.text
├── 1.text
└── bar
├── 1 egg.text
└── 3test.text
This is "additional comment" and not a complete answer by itself.
This is a really long way of 'explaining' a minor concept - it will appear long and pedantic except if the reader has missed the point.
Short summary:
If there are more than one file of name like foo.txt[(n)]
foo.txt, foo.txt(1) -- rename -- > no change
Appears that nothing happened. It did.
foo.txt(1), foo.txt(2) -- rename --> foo.txt, foo.txt(2) (probably)
One file renamed, one not.
Longer:
You say that you do not have duplicate files.
You may be correct
BUT - the following is trivially obvious if you know it but a subtle trap if you have not met it before.
As far as the system is concerned
foo.txt = = foo.txt(1) = = foo.txt(2) = etc
regardless of file content or size.
ie If you have two files names foo.txt with or without (n) after them then the system thinks they are duplicates and/or they are duplicates.
Either way, a rename process will fail.
If the rename process would create two files with the same name then it will fail when the attempt is made to create the 1st-duplicate. But
In one case the failure will not create any output so it will appear that "nothing happened" when really "copying failed due to name collision" happened.
This case occurs when one file has no (n) suffix and one other has.
eg if existing files are foo.txt and foo.txt(1) then the rename of foo.txt(1) would create a duplicate if allowed so will not occur, and so no action will take place.
foo.txt, foo.txt(1) -> no change
But if existing files are eg foo.txt(1) and foo.txt(2) then the rename of foo.txt(1) would NOT create a duplicate so will occur BUT the rename of foo.txt(2) subsequently would create a duplicate if allowed so no second-file-rename action will take place.
foo.txt(1), foo.txt(2) -> foo.txt, foo.txt(2) (probably)
foo.txt(1)
,foo.txt(2)
, which one should overwrite the other? – terdon Jun 10 '15 at 9:57