Add part of directory name to filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add part of directory name to filename
# 8  
Old 10-17-2016
The code you posted is adding all the names of folder

Code:
cops21_cops22_cops23_cops24_cops_25_HMCBackup_20161011.123704.tgz

---------- Post updated at 03:32 PM ---------- Previous update was at 03:12 PM ----------

Code:
basename `dirname "/nim/dr/HMC/cops21/HMCBackup_20161011.123704.tgz"`

is giving me cops21

Now, I need to append that cops21 to the filename
# 9  
Old 10-17-2016
Can you try this script and let me know how it goes ?

Code:
$ tree
.
├── cops21
│   └── HMCBackup_20161011.123704.tgz
├── cops25
│   └── HMCBackup_20161012.12346.tgz
└── try.sh

Code:
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

Code:
$ ./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 )

Code:
$ tree
.
├── cops21
│   └── cops21_HMCBackup_20161011.123704.tgz
├── cops25
│   └── cops25_HMCBackup_20161012.12346.tgz
└── try.sh


Last edited by greet_sed; 10-17-2016 at 04:39 PM.. Reason: Add tags
This User Gave Thanks to greet_sed For This Post:
# 10  
Old 10-17-2016
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

Code:
cops21_HMCBackup_20161011.123704.tgz

becomes
Code:
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
# 11  
Old 10-17-2016
Try changing
Code:
find . -name "*tgz"

to
Code:
find . -name "HMC*tgz"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the part of a filename

Hi, I am writing an ebuild for Gentoo Linux operating system. Writing an ebuild is about Bash scripting where I am a newbie. So, my ebuild must find a part of a specific filename. Such a filaname my look like this: libvclient_release_x64.so.740and I must to find the number at the and of... (18 Replies)
Discussion started by: csanyipal
18 Replies

2. Shell Programming and Scripting

Bash to calculate average of all files in directory and output by part of filename

I am trying to use awk to calculate the average of all lines in $2 for every file in a directory. The below bash seems to do that, but I cannot figure out how to capture the string before the _ as the output file name and have it be tab-delimeted. Thank you :). Filenames in... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. UNIX for Beginners Questions & Answers

Add part of pathname/subfolder name to beginning of filename

Hello everyone, I need some help renaming files. I have a directory with a bunch of subfolders in it like so with DWI being the working directory and the 3 levels after it being subdirectories home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz... (8 Replies)
Discussion started by: azurite
8 Replies

4. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

5. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

8. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

9. Shell Programming and Scripting

detecting the part of a filename

I like to have the date in the 2008-09-01 format at the beginning of my filenames. I then hyphenate after that and then have my filename. I have a script that creates this for me. However, I may be working on files that already have the date format already in there and so I don't want to have a... (4 Replies)
Discussion started by: mainegate
4 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question