Hi,
I am having a files in my directory like this:
2014 1049_file1.txt
2014 1050_file2.txt
2014 1110_file3.txt
2014 1145_file4.txt
2014 2049_file5.txt
I need to replace the above file names like this without changing the content of filename:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
I tried something like this but didn't workout:
for i in *.txt
do
mv $i ${*_i}
done
Hi
its not working its throwing an error
-bash: ${*_i}: bad substitution
Try this:
#!/bin/bash
for i in "*.txt"
do
mv $i ${i#*_}
done
Hi
it didn't replace the file with your code
sea
September 29, 2014, 11:17am
6
Try:
for f in *txt;do
tui-echo "$f" "${f/*_/}"
done
# | 2014 2014_file1.txt file1.txt | #
# | 2014 2014_file2.txt file2.txt | #
# | 2014 2014_file3.txt file3.txt | #
# | 2014 2014_file4.txt file4.txt | #
# | 2014 2014_file5.txt file5.txt | #
# | 2014 2014_file6.txt file6.txt | #
# | 2014 2014_file7.txt file7.txt | #
+ ~/tmp $
Of course you need to replace tui-echo
with mv
This will work:
for i in *.txt
do
mv "$i" "${i#*_}"
done
---------- Post updated at 05:25 PM ---------- Previous update was at 05:20 PM ----------
The problem here is that all txt files will expand, but within the double quotes, thus i
becomes one long string containing a list of all text files instead of handling/representing each file separately.
Hope this helps.
rohit_shinez:
Hi,
I am having a files in my directory like this:
2014 1049_file1.txt
2014 1050_file2.txt
2014 1110_file3.txt
2014 1145_file4.txt
2014 2049_file5.txt
I need to replace the above file names like this without changing the content of filename:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
I tried something like this but didn't workout:
for i in *.txt
do
mv $i ${*_i}
done
Hello Rohit,
Following may help.
for i in 2014*.txt
do
a=`echo $i | awk '{gsub(/.*\_/,X,$i);print $i}'`
mv "$i" "$a"
done
It will take all files whose names are starting by 2014 as per your given input.
Thanks,
R. Singh
The above will work only if the file name with embedded whitespace is punctuated with escaped double-quotes otherwise it'd fail...
for i in *.txt
do
mv \"$i\" ${i#*_}
done
sea
September 29, 2014, 12:09pm
10
Why do you want to escape the required quotes?
Makes 0 sense to me..
Why not simply substitute using bash builtins, as shown in previous example....
for f in *txt;do
mv "$f" "${f/*_/}"
done
This will replace everything before '_' with '', represented by the 'non-existing-space' between the last '/' and the closing '}'.
1 Like
Corrected my script and it works fine for me-
[root@Shekhar folder]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file9.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file8.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file7.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file6.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file5.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file4.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file3.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file2.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file1.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 2014 2014_file0.txt
[root@Shekhar folder]# for i in *.txt; do mv "$i" "${i#*_}" ; done
[root@Shekhar folder]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Sep 30 02:44 file9.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file8.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file7.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file6.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file5.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file4.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file3.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file2.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file1.txt
-rw-r--r-- 1 root root 0 Sep 30 02:44 file0.txt
[root@Shekhar folder]#