LinuxQuestions.org
Review your favorite Linux distribution.
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 12-07-2009, 04:55 PM   #1
Nzo
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Rep: Reputation: 0
Loop through all files in a directory. Bash/Perl script?


Hi,

I am trying to loop through all the files in a directory, so I can encrypt them all with pdftk.

I am using cygwin.

The file names are as follows:

June_other_ID_data.pdf


So I am trying to create a script that does this (I am used to PHP):

Code:
ls -a | $file

for each $file{
    $pieces=explode("_", $file)

    pdftk $file output $pieces[0]_$i.pdf owner_pw foopass
    $i++;
}
Is it best to use perl/bash?

How would I write this in either?

Thanks!
 
Old 12-07-2009, 05:05 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
What does the pieces & explode stuff mean, where did $i come from?
 
Old 12-07-2009, 05:09 PM   #3
Nzo
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
explode just means put that filename into an array.

so
$pieces=explode("_", "june_this_info");

$pieces would now be an array:
[0]june [1]this [2]info

$i is the loop counter, should be set to 0 at the start of the loop, just because each month has 4 files. (june_01, june_02 etc).

Obviously the code is just a rough example of what I need but I have little experience of scripting in bash.
 
Old 12-07-2009, 05:40 PM   #4
Nzo
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
any help please?
 
Old 12-07-2009, 05:58 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Sounds like you want something like

Code:
for file in *.pdf
do
    cnt=0
    mth=$(echo $file|cut -d'_' -f1)
    pdftk $file output ${mth}_${cnt}.pdf owner_pw foopass
    (( cnt++ ))
done
I'm not entirely clear about the 4 files per mth and whether you are doing only one file per run.

You should bookmark/read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 12-08-2009, 12:53 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Nzo View Post
Obviously the code is just a rough example of what I need but I have little experience of scripting in bash.
You have explained the code to us by using pseudo-bash but it's so far from bash that I don't know enough about what you are trying to do to begin answering the question -- or even to begin asking for further information to clarify the problem. Please explain the problem in English.
 
Old 12-09-2009, 05:25 PM   #7
Nzo
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Code:
#!/bin/bash
cnt=0
for file in *.pdf
do
    mth=$(echo $file|cut -d'_' -f1)
    pdftk $file output all/${mth}_${cnt}.pdf owner_pw pass
    (( cnt++ ))
done
thanks Chris - using this code but it stops after 4 repetitions/4 files.

Why is it stopping after 4? I get no error output?


Catkin, what im trying to do in English:

I have a directory of PDF files.

The file name convention is:
"January_2342.pdf",
"January_file_3342.pdf",
"January_2442.pdf",
"January_letter_2342.pdf",
"January_file_2342.pdf",
"February_07.pdf",
"Febuary_file_2342.pdf".

The month will always be first.

I want to write a script to encrypt all these files, and keep the month, so my new encrypted files will be:
"january_01.pdf", "january_02.pdf" etc.

The command to encrypt a PDF is:
pdftk original.pdf output encrypted.pdf owner_user password
 
Old 12-09-2009, 05:32 PM   #8
Nzo
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Some of the files are PDF

shopt -s nocaseglob

fixed it. Thanks alot for the code Chris
 
Old 12-09-2009, 05:43 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
No worries. Feel free to click on the blue thumb
 
Old 12-09-2009, 08:09 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
no need to call external cut
Code:
cnt=0
for file in *.pdf
do
    IFS="_"; 
    set -- $file
    mth=$1
    unset IFS
    echo pdftk $file output all/${mth}_${cnt}.pdf owner_pw pass
    (( cnt++ ))
done
 
  


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 loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Bash script to access all files in a directory shinni Programming 5 04-24-2009 04:46 PM
bash script to check how many files in directory guest Programming 8 02-01-2009 12:55 AM
Traversing files in a given directory using a bash script kaujot Programming 7 03-07-2008 01:39 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM


All times are GMT -5. The time now is 01:41 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration