rename file by removing some part of the file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename file by removing some part of the file name
# 1  
Old 12-28-2009
rename file by removing some part of the file name

I am special requirements to rename file.

I have files with names like below:

1_firstname1_lastname1.html
2_firstname2_lastname2.html
3_fistname3_lastname2.html

I would like these file to be renamed as below

firstname1_lastname1.html
firstname2_lastname2.html
fistname3_lastname2.html

I would like to rename files by removing text before the first occurrence of ‘_' and ‘_'.

Is there any way with sed or awk to achieve this?
# 2  
Old 12-28-2009
This is pretty easy one:
Code:
#!/usr/bin
for file in *_*
do
        r_name=${file#*_}
        mv $file $r_name
done

# 3  
Old 12-28-2009
If you have the rename command, then the following is one of the easiest way of doing things.,

Code:
rename 's/\d_//' *.html


Code:
$ ls
1_firstname1_lastname1.html  2_firstname2_lastname2.html  3_fistname3_lastname2.html
$ rename 's/\d_//' *.html
$ ls
firstname1_lastname1.html  firstname2_lastname2.html  fistname3_lastname2.html

# 4  
Old 12-29-2009
Quote:
rename 's/\d_//' *.html
This code, some how not working for me. File names are no changed. they remain same.
I have rename on my Linux and used /bin/bash as defualt shell
# 5  
Old 12-29-2009
how about this ?

Code:
rename 's/[0-9]_//' *.html

# 6  
Old 12-30-2009
To remove anything before the first _
Code:
rename 's/^[^_]*_//' *_*.html

But I would use freelong's for loop example..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

How to rename last part of file ?

Hi, I have large number of files like below - UNIX FY17 D21-1c Active user audit - NPP dir owner listing(vctmstt01)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - TTMS dir owner listing(pvcdmot35)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - PCP dir owner... (3 Replies)
Discussion started by: solaris_1977
3 Replies

3. Shell Programming and Scripting

Find and rename part of a file

hi, Need your help. I need to write a script for below.. i have two files in directory /home/abc as below: Watch_20140203_abc.dat Watchnow_20140203_abc.dat I have to copy this file from /home/abc to /home01/home02 after that i have to rename the date part in above two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

4. Shell Programming and Scripting

removing part of a file

Right this is quite a long one, I have a script which complies all listed stats files into one file and emails it out, However this has to be run manually and i would like it to run automatically, I have a list of files eg sa17 sa18 sa19 sa20 sa21 one file for each of last weeks... (9 Replies)
Discussion started by: Bdoydie
9 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

Removing part of a file name and appending into a single file

I have two files like ABC_DEF_yyyyymmdd_hhmiss_XXX.txt and ABC_DEF_yyyyymmdd_hhmiss_YYY.txt. The date part is going to be changing everytime. How do i remove this date part of the file and create a single file like ABC_DEF_XXX.txt. (8 Replies)
Discussion started by: varlax
8 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

8. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

9. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question