Bash script to execute a program to rename files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash script to execute a program to rename files
# 1  
Old 04-07-2012
Bash script to execute a program to rename files

I just can't figure it out , so please just give me a pice of advise how to:

The existing Linux program foo2bar takes as its only argument the name of a single foo file and converts it to an appropriately-named bar file. Provide a script that when executed will run foo2bar against all foo files in the current directory.
# 2  
Old 04-07-2012
In that script you need to call the program foobar for every foo file in a directory, so assuming these files have an extension of .foo you would need to do something like this:

Code:
cd somedir
for file in *.foo
do
  /path/to/foobar "$file"
done

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-07-2012
Ok, thank you for your answer, I'm still a little bit confused because the line containing for file in *foo I think it will be valid for all files in the directory ending with foo. What about the files starting with foo and ending with smth else (Ex: .tex) ? After that I suppose /path/to/foo2bar should be the path to the program that should convert(rename) foo files in bar files. but what shuold "$file" means?

Sorry for beeing so underdeveloped and behind the times but I really haven't touched scripting for years.
# 4  
Old 04-07-2012
If they are starting with foo then use:
Code:
for file in foo*

$file is the arbitrary variable in the for loop and each time it contains the name of the next foo file it finds in the current directory ..

Last edited by Scrutinizer; 04-07-2012 at 10:52 AM..
# 5  
Old 04-07-2012
Thank you, very nice answer Smilie I was almost sure it's some loop think... I'll dig deeper into the stuff now. Great help with your advise!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to rename files in bash loop

I am trying to use sed to rename all .txt files in /home/cmccabe/test. However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you :). bash # rename classified cd /home/cmccabe/test pattern2_old="_classify"... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

3. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

4. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

5. UNIX for Dummies Questions & Answers

Split and Rename files using Terminal and bin/bash

I have a file named Me_thread_spell.txt that I want to split into smaller files. I want it to be split in each place there is a ;;;. For example, blah blah blah ;;; blah bhlah hlabl awasnceuir asenduhfoijhacseiodnbfxasd;;; oabwcuhaweoir;;; This full file would be three separate files... (7 Replies)
Discussion started by: mschpers
7 Replies

6. Programming

C program to execute shell script

Hi, Can anyone give me a sample code to execute shell script from C program. Thanks (6 Replies)
Discussion started by: baigmd
6 Replies

7. Shell Programming and Scripting

C program to execute shell script

Hi, Can anyone pls give a sample to execute a shell script from C program Thanks (2 Replies)
Discussion started by: baigmd
2 Replies

8. Shell Programming and Scripting

How to execute a program at expect script

what i need is that after passwordless enter another program should execute. I can succeed passwordless login but ı could not execute (./son) program. pls help me (6 Replies)
Discussion started by: fozay
6 Replies

9. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

10. Shell Programming and Scripting

Hello - new here - bash script - need to rename and zip files.

I'm working on a project that basically unzips three zip files. When these unzip they create about 70+ directories with subdirectories of year/month with about 3 to 9 pdf files in each directory. Basically, I'm needing to figure out a way to zip these pdf files up. for instance the script... (1 Reply)
Discussion started by: Aixia
1 Replies
Login or Register to Ask a Question