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
# 1  
Old 10-17-2016
Add part of directory name to filename

Hello,

I need to add a part of folder name to the files inside it. For instance the file is

Code:
HMCBackup_20150430.155027.tgz

and it is under directory
Code:
/nim/dr/HMCBackup/cops22

I need to add cops22 to the file name so as it would be
Code:
cops22_HMCBackup_20150430.155027.tgz

Any help in doing this would be appreciated


Thanks
# 2  
Old 10-17-2016
Hi,

can you try this ?

Code:
A=/nim/dir/HMC/cops22
$ echo ${A##*/}
cops22

# 3  
Old 10-17-2016
Quote:
Originally Posted by hasn318
Hello,
I need to add a part of folder name to the files inside it. For instance the file is
Code:
HMCBackup_20150430.155027.tgz

and it is under directory
Code:
/nim/dr/HMCBackup/cops22

I need to add cops22 to the file name so as it would be
Code:
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/cops22only, then following may help you in same.
Code:
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.tgzto 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
# 4  
Old 10-17-2016
Code:
./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.
Code:
/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

Code:
cops21_HMCBackup_20161011.123704.tgz
cops22_HMCBackup_201610110.134524.tgz

and so on.
# 5  
Old 10-17-2016
Linux/bash
Code:
cp filename `echo "${PWD##*/}_filename"`

# 6  
Old 10-17-2016
Code:
cp file `echo "{PWD##*/}_filename"`
cp: file: No such file or directory


Code:
cp HMCBackup_20161011.123704.tgz `echo "{PWD##*/}_filename"`
cp: {PWD##*/}_filename: No such file or directory

# 7  
Old 10-17-2016
Quote:
Originally Posted by hasn318
Code:
cp file `echo "{PWD##*/}_filename"`
cp: file: No such file or directory

Code:
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.
Code:
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.
Code:
while read line
do
	cd "$line"
	for file in *.tgz
	do 
		mv "$file" "${line##*/}_$file"
	done
done < "path_name"

Thanks,
R. Singh
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