Reputation: 25
I have file names like this:
MOD13Q1.A2013001.h25v08.005.2013018031021.hdf.250m_16_days_EVI.tif
MOD13Q1.A2013001.h25v08.005.2013018031021.hdf.250m_16_days_NDVI.tif
MOD13Q1.A2013001.h25v08.005.2013018031021.hdf.250m_16_days_VI_Quality.tif
MOD13Q1.A2013017.h25v08.005.2013039200748.hdf.250m_16_days_EVI.tif
MOD13Q1.A2013017.h25v08.005.2013039200748.hdf.250m_16_days_NDVI.tif
MOD13Q1.A2013017.h25v08.005.2013039200748.hdf.250m_16_days_VI_Quality.tif
I have need only specific details within the existing file name. I want the file names to be renamed like : A2013001_h25v08_EVI.tif
I have used the code
for f in *.tif
do
c1= cut -c9-16 $f
c2= cut -c18-23 $f
c3= cut -c60-80 $f
c= echo $c1$c2$c3
echo mv "{$f}" "{$c}"
done
But this code does not work. Is there another better method to do this? I am still new to Linux coding and hence any suggestions will be of great help.
Thanks
Upvotes: 0
Views: 92
Reputation: 91099
If you have mmv
installed, just do
mmv MOD13Q1.\*.*.005.2013039200748.hdf.250m_16_days_.*.tif \#1#2#3.tif
About your code:
for f in *.tif
do
c1= cut -c9-16 $f
c2= cut -c18-23 $f
c3= cut -c60-80 $f
Here, the backticks are missing:
c1=`echo "$f" | cut -c9-16`
c2=`echo "$f" | cut -c18-23`
c3=`echo "$f" | cut -c60-80`
Alternatively, you could use
c1=$(echo "$f" | cut -c9-16)
c2=$(echo "$f" | cut -c18-23)
c3=$(echo "$f" | cut -c60-80)
c= echo $c1$c2$c3
Here,
c=$c1$c2$c3
is enough.
Some other notes: You should become accustomed to using "
around your variables unless explicitly not wanted.
If you had files like
MOD13Q1 A2013001 h25v08 005 2013018031021 hdf 250m_16_days_EVI.tif
(which would be perfectly valid and legitime)
your code would fail to work with them:
for i in *.tif; do
mv "$i" "$i".bak
mv $i $i.bak
end
would do two completely different things: the first one would actually rename the file, whicle the latter one would try to execute
mv MOD13Q1 A2013001 h25v08 005 2013018031021 hdf 250m_16_days_EVI.tif MOD13Q1 A2013001 h25v08 005 2013018031021 hdf 250m_16_days_EVI.tif.bak
which means to move/rename the files MOD13Q1
, A2013001
, h25v08
, 005
, 2013018031021
, hdf
, 250m_16_days_EVI.tif
, MOD13Q1
, A2013001
, h25v08
, 005
, 2013018031021
and hdf
to 250m_16_days_EVI.tif.bak
.
Upvotes: 1
Reputation: 328724
Use mmv
:
mmv -n "MOD13Q1.*.005.*.hdf.250m_16_days_*.tif" "#1_#3.tif"
If the output looks correct, remove the -n
to actually rename the files.
Note: mmv
isn't a standard command, you may have to install it using the system's package manager.
Upvotes: 1