Hello,
I need to add a part of folder name to the files inside it. For instance the file is
HMCBackup_20150430.155027.tgz
and it is under directory
/nim/dr/HMCBackup/cops22
I need to add cops22 to the file name so as it would be
cops22_HMCBackup_20150430.155027.tgz
Any help in doing this would be appreciated
Thanks
Hi,
can you try this ?
A=/nim/dir/HMC/cops22
$ echo ${A##*/}
cops22
hasn318:
Hello,
I need to add a part of folder name to the files inside it. For instance the file is
HMCBackup_20150430.155027.tgz
and it is under directory
/nim/dr/HMCBackup/cops22
I need to add cops22 to the file name so as it would be
cops22_HMCBackup_20150430.155027.tgz
Any help in doing this would be appreciated
Thanks
Hello hasn318,
So let's say you are in path /nim/dr/HMCBackup/cops22
only, then following may help you in same.
cat script.ksh
PWD=`pwd`
VAL=$1
mv $VAL "${PWD##*/}_$VAL"
You could run script.ksh HMCBackup_20150430.155027.tgz
and it will change file HMCBackup_20150430.155027.tgz
to cops22_HMCBackup_20150430.155027.tgz
, you could pass file name which is present in path and it will rename it as above.
Thanks,
R. Singh
./test: line 3: $: command not found
./test: line 4: cops22: command not found
I get this error
---------- Post updated at 02:37 PM ---------- Previous update was at 02:28 PM ----------
Actually I have five different folders and files in it that needs to be changed.
/nim/dr/HMC/cops21
/nim/dr/HMC/cops22
/nim/dr/HMC/cops23
/nim/dr/HMC/cops24
/nim/dr/HMC/cops25
and the files should be named
cops21_HMCBackup_20161011.123704.tgz
cops22_HMCBackup_201610110.134524.tgz
and so on.
looney
October 17, 2016, 2:40pm
5
Linux/bash
cp filename `echo "${PWD##*/}_filename"`
cp file `echo "{PWD##*/}_filename"`
cp: file: No such file or directory
cp HMCBackup_20161011.123704.tgz `echo "{PWD##*/}_filename"`
cp: {PWD##*/}_filename: No such file or directory
Hello hasn318,
Let's say you have saved all paths into a file named path_names
.
cat path_names
/nim/dr/HMC/cops21
/nim/dr/HMC/cops22
/nim/dr/HMC/cops23
/nim/dr/HMC/cops24
/nim/dr/HMC/cops25
Could you please try following code and let me know how it goes then.
while read line
do
cd "$line"
for file in *.tgz
do
mv "$file" "${line##*/}_$file"
done
done < "path_name"
Thanks,
R. Singh
The code you posted is adding all the names of folder
cops21_cops22_cops23_cops24_cops_25_HMCBackup_20161011.123704.tgz
---------- Post updated at 03:32 PM ---------- Previous update was at 03:12 PM ----------
basename `dirname "/nim/dr/HMC/cops21/HMCBackup_20161011.123704.tgz"`
is giving me cops21
Now, I need to append that cops21 to the filename
Can you try this script and let me know how it goes ?
$ tree
.
cops21
HMCBackup_20161011.123704.tgz
cops25
HMCBackup_20161012.12346.tgz
try.sh
cat try.sh
#!/bin/bash
find . -name "*tgz" |
while read line
do
echo $line
getext=${line##*.}
getdir=${line%/*}
getdir=${getdir#./*}
justfile=${line#./*/}
justfile=${justfile%.*}
mv $line ./${getdir}/${getdir}_${justfile}.${getext}
done
$ ./try.sh
./cops25/HMCBackup_20161012.12346.tgz
./cops21/HMCBackup_20161011.123704.tgz
After running the script, i get the output as you said.( renamed files )
$ tree
.
cops21
cops21_HMCBackup_20161011.123704.tgz
cops25
cops25_HMCBackup_20161012.12346.tgz
try.sh
1 Like
hasn318
October 17, 2016, 4:26pm
10
That works!!
---------- Post updated at 04:26 PM ---------- Previous update was at 03:46 PM ----------
One thing I noticed is that every time I run the try.sh script, it keeps adding the cops21 to the file name
cops21_HMCBackup_20161011.123704.tgz
becomes
cops21_cops21_HMCBackup_20161011.123704.tgz
that should not be the case, it should only change files which are HMC*.tgz and not cops21_HMC*.tgz
thanks