LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 09-19-2012, 11:52 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Rep: Reputation: Disabled
Rename a file with parentdir name


Dear all,

I have several ABC.ps files that I would like to transform into pdf for visualization reasons. Each of them is in a different directory and all of them are called exactly ABC.ps:

/../alpha/ABC.ps
/../beta/ABC.ps
/../gamma/ABC.ps
..

I want them to have the directory name as filename, so that it will be easy for me to pick up the ones I need to see with acroread, once ps2pdfed them.

Would you use mv for example? But how can I tell it to get the dirname?
I've tried several options, including baseline pwd` but with no wayout.

I would start with:

Code:
for i in ${ls */ABC.ps}
do
CHANGE NAME $i
ps2pdf $i
done
or the other way round
Code:
for i in ${ls */ABC.ps}
do
ps2pdf $i
CHANGE NAME $i
done
I would strongly appreciate any help.

Sincerely,

Udiubu
 
Old 09-19-2012, 12:04 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,703
Blog Entries: 54

Rep: Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964Reputation: 2964
Code:
find /path/to/files -type f -iname \*.ps -printf "echo mv \"%p\" \"%h/\$(basename %h)_%f\"\n"
remove the "echo " if you think it looks alright then pipe through a shell or redirect output to a file and run that as a script?
 
1 members found this post helpful.
Old 09-19-2012, 12:09 PM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
Don't use ls to feed a for loop. It can be hazardous for your health
I would do it this way:
Code:
# This is where all PDFs will be put
destDir=/path/where/to/put/the/pdfs
mkdir -p "$destDir"

# This is where to search for the ps files
rootPSDir=/path/to/the/root/containing/all/ps/files

find "$rootPSDir" -name ABC.ps | while read file
do
	fullPath="${file%/*.ps}"
	parentDir="${fullPath##*/}"
	ps2pdf "$file" "$destDir/$parentDir.pdf"
done
 
1 members found this post helpful.
Old 09-19-2012, 12:31 PM   #4
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
414N, it works beautifully!
So to clarify:

Quote:
fullPath="${file%/*.ps}" # what does "%/" stand for here?
parentDir="${fullPath##*/}" # "##" this generally means 'end of file', right? So in this case, "stop to parDir name?
Thanks to both of you for this precious help.

Best,

Udiubu
 
Old 09-19-2012, 12:45 PM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
Those are special parameters expansion (you can find more info about them and the like under the "PARAMETER EXPANSION" section of the man page for bash) used to remove some patterns.
Specifically:
  • ${A%/*.ps} removes the least long "/*.ps" pattern (where * matches every character) from the end of the string contained inside the shell variable A. If A=/asd/fgh/xzc/qwe/abc.ps, then the result is /asd/fgh/xzc/qwe, because * matched abc and so the pattern used for the removal expanded to /abc.ps.
  • ${A##*/}, on the other hand, removes the longest "*/" pattern (because we're using the double #) from the start of A's contents. If A=/asd/fgh/xzc/qwe/abc.ps, then the result is abc.ps.
Hope this was clear enough.
 
Old 09-19-2012, 12:48 PM   #6
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Wow, well clear.
 
  


Reply

Tags
dir, mv, rename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 03:49 AM
[SOLVED] How do I rename a file in C? stf92 Programming 2 07-21-2012 11:58 PM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 02:09 PM
Need to replace a file with a newer version but keep the old file via a rename Joe_P Linux - Newbie 4 04-30-2009 04:51 PM
how to rename a file and copy a file in a shell zach014 Linux - Newbie 6 11-23-2006 10:23 AM


All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration