Rename mass files with text from first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename mass files with text from first line
# 1  
Old 06-01-2012
Rename mass files with text from first line

I have a few hundred text files that are currently numbered files. I would like to rename each one with the text from the first line in the file. I would prefer this is perl script rather than a one liner as it wil be after many alterations to the file via an existing script. Any help would be greatly appreciated.
# 2  
Old 06-01-2012
Try this:

Code:
for file in *
do
   # Avoid renaming diretories!
   if [ -f "$file" ]
   then
       newname=`head -1 $file`
       if [ -f "$newname" ]
       then
              echo "Cannot rename $file to $newname - file already exists"
       else
              mv "$file" "$newname"
       fi
   fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

2. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

3. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

4. UNIX for Dummies Questions & Answers

Need help to mass rename files

Hi. I've got 75 mp3s that have the word 'Émission' in their filename. They are all in this format: Émission bla1 bla1.mp3 Émission bla2 bla2.mp3 Émission bla3 bla3.mp3 etc... I would just like to mass replace 'Émission' by 'Emission'; basically replace 'É' with 'E'. The rest of the... (10 Replies)
Discussion started by: Kingzy
10 Replies

5. Shell Programming and Scripting

Compare two text files line by line

Hi all I need help on comparing two texts files line by line and print the total number of lines matched. Any help is appreciated. Thanks in advance. (4 Replies)
Discussion started by: my_Perl
4 Replies

6. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

7. Shell Programming and Scripting

bash script to rename in mass

Basically, I have a huge amount of files (ripped audiobooks) that all have the same garbage in their filenames. I'm wondering how to go about writing a bash script to mass rename them. Example filenames as they stand now: The First CD - 1x01 - Title 1.mp3 The First CD - 1x02 - Title 2.mp3... (4 Replies)
Discussion started by: audiophile
4 Replies

8. AIX

VI questions : mass changes, mass delete and external insert

Is it possible in VI to do a global change but take the search patterns and the replacement patterns from an external file ? I have cases where I can have 100,200 or 300+ global changes to do. All the new records are inside a file and I must VI a work file to change all of them. Also, can... (1 Reply)
Discussion started by: Browser_ice
1 Replies

9. UNIX for Dummies Questions & Answers

Mass Copy/rename

Don't tell me DOS can do something UNIX can't do! I want to copy a number of files from one directory to another, and at the same time change the names. The name changes would be common, e.g., all files starting with the letter 'L' and ending in '30.NEW554', with the copied or new files also... (6 Replies)
Discussion started by: lwilsonFG
6 Replies

10. UNIX for Dummies Questions & Answers

Easy way to mass rename files?

Hi. What is the easiest way to rename a bunch of files? For example taking all files ending in ".php3" and rename them to end in ".php" I could write a script to do this, but there is probably an easier way... Thanks! (17 Replies)
Discussion started by: Thermopylae
17 Replies
Login or Register to Ask a Question