rename a lot of files again


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename a lot of files again
# 1  
Old 03-30-2008
rename a lot of files again

here I go again...kinda hard to explain so I apologize.
I need to rename a bunch of files in a directory. I need to remove the first three characters of the filename, and then toward the end of the filename there is constant text inside of brackets. here is a demo (not for real) 'ls -1' of the dir:


001 Random name here - random title here - [constanttxt] f g- f.txt
002 another random name here - more random txt here [constanttxt] dg .txt
003 this is 3rd random name - 3rd random-title[constanttxt] d- df .txt
.
.
.
100 and and and the hundred names - hundred titles [constanttxt] s a .txt


Ok well its something like that, pretty ugly filenames! Anyway for now I just need the begining three numbers removed from each filename, and the [constanttxt] removed. They are all .txt files. Notice near the end of each filename the spacing is kinda random also, sometimes theres even random hyphens in the titles. Hope it makes sense. this is Fedora8, bash shell, and I have played around with awk and sed, but awk and sed dont seem to play nice with the bracket [] characters? Not only that but the mv and/or cp commands dont seem to play nice with spaces in the filenames thus I've really lost hope of automating this! Any pointers would help! Thank you so much!!!

Last edited by ajp7701; 03-30-2008 at 09:28 PM..
# 2  
Old 03-30-2008
example from research

I found this script that seems to be doing something remotely similar what i want to do, it is renaming a bunch of files with numbers at the front...so I can probably tweak on it a bit and get close to what desired, but still my fight is with the [] and the spacing in the filenames.....
ps, whoever wrote this good job!!

#!/bin/sh
ls |
egrep "^[0-9][0-9][0-9][0-9][0-9]" |
while read a
do
yahoo=`echo $a |sed 's/^[0-9][0-9][0-9][0-9][0-9][0-9][ .]//gi' | sed 's/^[kms]b[cs][^.]*\.//gi'`
echo $yahoo
mv "$a" "$yahoo"
done
# 3  
Old 03-30-2008
Hi,

The filenames are not pretty, so I'd suggest to keep those files in one directory or rename them appropriately. Assuming all and only of the above files are in one directory :

Code:
for i in *
do 
   mv "$i" "$(echo "$i" | awk '{gsub(/\[constanttxt\]/,""); $1=""}1')"
done

Tested on Fedora 8.

Last edited by rubin; 03-30-2008 at 10:30 PM.. Reason: added : only of the ...
# 4  
Old 03-30-2008
Here is my solution....
Code:
$ cat fix
#! /usr/local/bin/bash

shopt -s extglob

ls |while IFS="" read name ; do
        if [[ $name = *.txt ]] ; then
                newname=${name##+([0-9]) }
                newname=${newname/\[*\]/}
                mv "$name" "$newname"
        fi
done
$
$
$
$
$ ls -1
001 Random name here - random title here - [constanttxt] f g- f.txt
002 another random name here - more random txt here [constanttxt] dg .txt
003 this is 3rd random name - 3rd random-title[constanttxt] d- df .txt
c
fix
$
$
$
$
$ ./fix
$
$
$
$ ls -1
Random name here - random title here -  f g- f.txt
another random name here - more random txt here  dg .txt
c
fix
this is 3rd random name - 3rd random-title d- df .txt
$

# 5  
Old 03-31-2008
thx!

thanks! that looks great. Hey perderabo even though I dont recognize the shopt -s extglob command your output looks great! thx!! one of my problems was i didnt know how to deal with the [brackets] SO THANK YOU THANK YOU THANKYOU!!!!
# 6  
Old 03-31-2008
oops...

Perderabo's script:
Ok the script works great...but as it turns out for many of my files there is also a hyphen between the first three digit number and the name....so the mv command goes to move it to the $newname which then begins with a hyphen and gives an error 'unknown option --'
How do I deal with that one?? :-)
thx again for the help! The script was awesome with all the ##+([0-9 stuff! dang I never would have figured that out.
I need to find a good reference for all these special characters and what they mean.
# 7  
Old 03-31-2008
Try:
mv ./"$name" ./"$newname"

./name is the same as just name so it should work, but I haven't tried it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

Convert a lot of files in subdirectories automatically

Hi, I have a huge structure of directories and subdirectories contsining some data. The lowest folders contain a file "image.png" which need to be converted to "folder.jpg". But how can I do that for all these files automatically? That's what I alredy have find /path -type f -name... (1 Reply)
Discussion started by: KarlKarpfen
1 Replies

3. Shell Programming and Scripting

Need a script for automation the convert a lot number audio files to another format

I have a lot number audio files in the MP3 proprietary format, I want to convert them to 'opus' the free and higher quality format, with keep metadata also. My selection command-line programs are SoX (Sound eXchange) for convert MP3 files to 'AIFF' format in order to keep quality and metadata*... (1 Reply)
Discussion started by: temp-usr
1 Replies

4. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

5. UNIX for Dummies Questions & Answers

Lot of warn files filling /

hi guys I have suse 11 sp1 and I have a lot of warn file filling / these are under /var/log there's this big one -rw-r----- 1 root root 3.9G Feb 1 10:28 warn warn: ASCII text and the others that are about 2.5 to 3MB - they are about 130 warn-*.bz2 -rw-r----- 1 root root 3.9G Feb... (2 Replies)
Discussion started by: karlochacon
2 Replies

6. Shell Programming and Scripting

Need to modify a lot of html files

Hello, I have about 3400 files in a tree structure (about 80% are html files). 1. I need to modify every html file to remove <p> style and old things like font attribute and add another style. 2. I need to change the root of all links that are in the html. e.g. change /old/path/ to /new/path... (1 Reply)
Discussion started by: Yaazkal
1 Replies

7. Shell Programming and Scripting

Rename a lot of files using shells script

Hi This is the list file that i have : The files is more than this. I will rename one by one file become like this : So just change the time stamp 200906 become 200905. Is it possible using script ? Thanks (3 Replies)
Discussion started by: justbow
3 Replies

8. Shell Programming and Scripting

chmod a lot of files

So i have about 600gb of data.. in which there are alot of directories and alot of files.. Im trying to put this on a ftp server.. So i want to set the permissions on the directories to be 755 and the permission on the files to be 644. So i used: find . -type d -exec chmod 755 {}\; and find .... (6 Replies)
Discussion started by: supermiguel
6 Replies

9. UNIX for Dummies Questions & Answers

Sorting with unique piping for a lot of files

Hi power user, if I have this file: file1.txt: 1111 1111 2222 2222 3333 3333 3333 4444 4444 4444 when I run the sort file1.txt | uniq > data1.txt the result is (2 Replies)
Discussion started by: anjas
2 Replies

10. Shell Programming and Scripting

Problem comparing 2 files with lot of data

Hello everyone, here's the scenario I have two files, each one has around 1,300,000 lines and each line has a column (phone numbers). I have to get the phones that are in file1 but not in file2. I can get these phones trough Oracle but my boss does not want that so he gave me the files with the... (4 Replies)
Discussion started by: rafisha
4 Replies
Login or Register to Ask a Question