943,724 Members | Top Members by Rank

Ad:
Sep 5th, 2006
0

Processing file by file in a txt, and storing the name of the file being processed

Expand Post »
Hi!,
I'm kinda new to shell scripting, Here's a problem that I have faced and dunno how to solve:
First,
I want my script to rename the files stored in a .txt file line by line(there's no pattern that I can use with mv, so I want mv to deal with a file generated by ls /home/whatever/ > whatever_files.txt). So, Let's that that now mv is dealing with the first line in whatever_files.txt which has a file name /home/whatever/whatever545.jpg, I want the new file to be called file_whatever.jpg. But in order to do that, I figured out that I must store the name of the file in a variable and having a mv command which looks like this:
mv whateverhey.jpg file_$filename

but I dunno how to store that name in a variable and how to make mv deal with files listed in a text file.

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alphabeta is offline Offline
3 posts
since Sep 2006
Sep 6th, 2006
0

Re: Processing file by file in a txt, and storing the name of the file being processed

I figured out that I can a non C-style loop to work with all the files in directory but when I write this it doesn't work:
for filename in *
do
mv $filename hello_$filename
done

????

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alphabeta is offline Offline
3 posts
since Sep 2006
Sep 6th, 2006
0

Re: Processing file by file in a txt, and storing the name of the file being processed

Shell Scripting Syntax (Toggle Plain Text)
  1. cd /to/my/directory
  2. find . -name '*.jpg' | \
  3. while read file
  4. do
  5. mv $file "new"$file
  6. done
PS: mv works only within a filesystem. Even though you may be
in directories that are close in terms of directory trees does not mean there are no intervening mountpoints. -
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Sep 6th, 2006
0

Re: Processing file by file in a txt, and storing the name of the file being processed

I indeed sometimes try to access some directories behind mount points(for example, hdb1 mounted on /home/user/myfolder), while the script is being executed from the user home directory, Do you mean that this can result in some problems?

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alphabeta is offline Offline
3 posts
since Sep 2006
Sep 8th, 2006
1

Re: Processing file by file in a txt, and storing the name of the file being processed

In that case you have to use cp. mv does not mv over mount points.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Interacting with other Applications via dtksh
Next Thread in Shell Scripting Forum Timeline: Making a shell script executable from any directory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC