sed to rename files in bash loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to rename files in bash loop
# 1  
Old 03-20-2017
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 Smilie.

bash
Code:
# rename classified
cd /home/cmccabe/test
pattern2_old="_classify"
pattern2_new="hg19multianno"

for file in *.txt
do
        newname=`printf '%s\n' "$file" | /bin/sed -e "s/$pattern2_old/$pattern2_new//"`
        /bin/mv -fv "$file" "$newname"
done
/bin/sed: -e expression #1, char 27: unknown option to `s'
‘17-0000_classify.txt’ -> ‘’
/bin/mv: cannot move ‘17-0000_classify.txt’ to ‘’: No such file or directory
/bin/sed: -e expression #1, char 27: unknown option to `s'
‘17-0001_classify.txt’ -> ‘’
/bin/mv: cannot move ‘17-0001_classify.txt’ to ‘’: No such file or directory


contents of /home/cmccabe/testbefore script executed

Code:
17-0000_classify.txt
17-0001_classify.txt
17-0002_classify.txt

contents of /home/cmccabe/testafter script executed (desired output)

Code:
17-0000_hg19multiannotxt
17-0001_hg19multianno.txt
17-0002_hg19multianno.txt


Last edited by rbatte1; 03-21-2017 at 09:50 AM.. Reason: Moved a closing CODE tag to make more sense
# 2  
Old 03-20-2017
Quote:
Originally Posted by cmccabe
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 Smilie.
You get the error because this:
Code:
sed -e "s/$pattern2_old/$pattern2_new//"

is not a valid sed-command. The s-subcommand has the form:

Code:
s/<regexp>/<regexp>/

and the last slash at the end leads to a simple syntax error.

Furthermore, your script is about as badly written as can be:

Code:
cd /home/cmccabe/test

Never do that in a script. Ideally you should use a commandline argument to provide the directory in question, but in absence of this do it this way:

Code:
dir="/home/cmccabe/test"

for file in "$dir"/* ; do
    mv $file ....
done

Secondly:
Code:
for file in *.txt
do
        newname=`printf '%s\n' "$file" | /bin/sed -e "s/$pattern2_old/$pattern2_new//"`
        /bin/mv -fv "$file" "$newname"
done

I spare you the obligatory sermon about not using backticks: you have been told this perhaps several dozen times already and i find the insistence to write bad scripts commendable. The POSIX variant of $(...) has only been developed to have something one can safely ignore.

You still may consider to invoke sed only once per run of the script instead of once per file the script encounters:

Code:
old="classify"
new="hg19multianno"

ls -1 "$dir"/*.txt |\
sed 's/\(.*\)'"$old"'\(.*\)/\1'"$old"'\2 \1'"$new"'\2/' |\
while read oldfile newfile ; do
     mv "$oldfile" "$newfile"
done

But even this is just a waste of processing power and other resources, because you can do the same with variable expansion inside the shell, without any external program:

Code:
old="classify"
new="hg19multianno"

ls -1 "$dir"/*txt |\
while read file ; do
     mv "$file" "${file/${old}/${new}}"
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 03-25-2017
Thank you very much for your help and explanations Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this... for i in new_* do if ] && ]; then j=`echo "$i"|cut -c5-` mv $i $j fi done ... (5 Replies)
Discussion started by: Korn0474
5 Replies

6. Linux

rename files using loop with different name

Hi, i need to write a shell script where i have to loop through all the file in a directory and rename them based on below condition. file1.dat file2.dat file3.dat the above files has to be moved to another directory like below file1_201001.dat file2_201002.dat file3_201003.dat... (3 Replies)
Discussion started by: feroz
3 Replies

7. UNIX for Dummies Questions & Answers

Rename files with sed in an until loop (double post)

I want to change the name of some of my files (mypics-0001, mypics-0002, mypics-0003.....mypics-0240) and I want to double check to see if this code is right: x=0 until do sed 's/mypics\-*/bday/g' done Would this change all of my file names to "bday0001....bday0240"? Please let me... (0 Replies)
Discussion started by: jvpike
0 Replies

8. UNIX Desktop Questions & Answers

Rename files without using for loop

Hi, Is it possible to rename files at a time in a directory without using the for loop. (ex: intial filename- abc.txt to abc.tmp or abc.txt.tmp) I need to rename the files in a remote directory to which I'm connecting thru tectia sftp. The commands 'mv', 'for', 'echo' do not work. I have... (3 Replies)
Discussion started by: Qwerty123
3 Replies

9. 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