LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-06-2012, 01:22 PM   #1
IlIl7
LQ Newbie
 
Registered: Oct 2010
Location: Europe mainly.. :D
Distribution: BackTrack 5
Posts: 17

Rep: Reputation: 0
Rename pdf files after their directory name and acoording to their total number


Hi
Given:
I have up to 1000 directories:
Quote:
dirA ...dirX ....etc
all of which are located inside the directory
Quote:
~/home/somestuff
Each one of the directories contains more than one pdf file with random names:
e.g.
Quote:
inside directory dirA there are the pdfs: koko.pdf
inside directory dirB there are the pdfs: ppko.pdf pig.pdf
inside directory dirX there are the pdfs: p2p2.pdf asas.pdf gigi.pdf qqq.pdf
Problem:
I need a command/script which when i run from somestuff will rename all pdfs according to both their parent folder name and their total number inside this folder.
That is I need to get:
Quote:
inside directory dirA: dirA.pdf
inside directory dirB: dirB1.pdf dirB2.pdf
inside directory dirX: dirX1.pdf dirX2.pdf dirX3.pdf dirX4.pdf
So far the following script works BUT ONLY IF I RUN IT INSIDE FROM EACH DIRECTORY:
Code:
c=0
for i in *.pdf
do 
   test -f "$i" &&\
   mv "$i" $(printf "${PWD##*/}%03i.pdf" $c) &&\
   ((c+=1))
done
I NEED TO MAKE IT WORK FOR EACH ONE OF THE DIRECTORIES WHEN I RUN IT FROM somestuff
i think its just some change..aint have the experience though
 
Old 07-06-2012, 02:15 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,417
Blog Entries: 55

Rep: Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616
Asserting:
0) no directory names contain spaces or other IFS b0rkage,
1) subdirectories are all in one root (here: /path/to/directories) and
2) at the same level and you
2) mod your function a bit (use 'find' instead of "for i in"),
you could:
Code:
myRename() { c=0; for i in *.pdf; do test -f "$i" && mv "$i" $(printf "${PWD##*/}%03i.pdf" $c) && ((c+=1)); done; }
cd /path/to/directories; find . -type d -maxdepth 1 | while read SUBDIR; do
 cd "${SUBDIR}" && myRename && cd ..
done
 
Old 07-06-2012, 03:48 PM   #3
IlIl7
LQ Newbie
 
Registered: Oct 2010
Location: Europe mainly.. :D
Distribution: BackTrack 5
Posts: 17

Original Poster
Rep: Reputation: 0
mmm it doesnt seem to work for all subdirectories...only for one...
any idea???
 
Old 07-06-2012, 04:09 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,417
Blog Entries: 55

Rep: Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616Reputation: 3616
No, I don't have a clue what you did as you posted nothing. On the CLI run 'set -vx' before running the whole thing again.
 
  


Reply


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
How to find the total number of files in a folder cnm Linux - Newbie 18 08-25-2015 01:01 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
rename all files in a directory merlin23 Linux - Newbie 4 03-24-2005 11:02 AM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 04:27 PM
Command to list total number of files. WillieB_72 Linux - General 3 01-29-2003 09:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:36 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
Open Source Consulting | Domain Registration