LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename a file with parentdir name (https://www.linuxquestions.org/questions/linux-newbie-8/rename-a-file-with-parentdir-name-4175428011/)

udiubu 09-19-2012 10:52 AM

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

unSpawn 09-19-2012 11:04 AM

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?

414N 09-19-2012 11:09 AM

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


udiubu 09-19-2012 11:31 AM

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

414N 09-19-2012 11:45 AM

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.

udiubu 09-19-2012 11:48 AM

Wow, well clear.


All times are GMT -5. The time now is 06:37 PM.