how to get rid of last _ in the files name?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to get rid of last _ in the files name?
# 1  
Old 04-11-2011
how to get rid of last _ in the files name?

ex: I have list of files in a folder.
Code:
abc_def_geh_.txt
abc_.txt
abc_def_geh_12345_.txt
ab134c_d345345ef_444geh_12345_.txt

i need to rename all files to get rid of the _ before .txt

result should look like this:
Code:
abc_def_geh.txt
abc.txt
abc_def_geh_12345.txt
ab134c_d345345ef_444geh_12345.txt


Last edited by Franklin52; 04-12-2011 at 03:39 AM.. Reason: Please use code tags
# 2  
Old 04-11-2011
Try this:

Code:
for file in *_.txt; do mv $file `echo $file | sed 's/_.txt/.txt/g'`; done

# 3  
Old 04-14-2011
a more general script to do this (handy because any sysadmin and a lot of users have to do this a number of times): http:// forums.macrumors.com/member.php?u=347558
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting rid of ^M

I have a text file with hundreds of 32-character hash codes in it, each terminated with a linefeed (/l, or ^M). 185ead08e45a5cbb51e9f7b0b384aaa2 57643e1a17252a9fc746d49c3da04168 60cba11d09221d52aaabb5db30f408a2 2b75ee6e5c2efc31b4ee9a190d09a4df ...... etc. I want to create a file for each... (6 Replies)
Discussion started by: teledon
6 Replies

2. Linux

How to get rid of ^m

Hi all, I am new to unix....pls help me with this. I have a binary file generating output by passing arguments in bash.when i open the output file in VI i can see that ^m is included in between most of lines,as a result when i pass this file to my java application it dosent parse the data... (3 Replies)
Discussion started by: asheshrocky
3 Replies

3. UNIX for Dummies Questions & Answers

How to get rid of ^[[D

Hi All, Im selecting a large record from a table and putting it in a file in the unix box. The file has a hidden character "^[[D " present in it. Can any one help me in getting rid of the character Thanks in advance, (4 Replies)
Discussion started by: madhan@29
4 Replies

4. Shell Programming and Scripting

Getting Rid of Having to Write to Flat Files

Ok, so i've been having to write to flat files lately and then making my script read information from the flat file and then work off of that. i dont want to keep doing that because i believe it creates a mess. i like to keep my work all to one script instead of having that one script... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. UNIX for Dummies Questions & Answers

how to get rid of ==>

ok the assignment question: That English paper you were writing on the works of Lewis Carroll is due in a few hours and you have forgeotten the name of the text file in which you has written a number of quotations to use in your paper. Luckily, you know that the file is somewhere in your... (1 Reply)
Discussion started by: mek86
1 Replies

6. UNIX for Dummies Questions & Answers

Getting rid of files with no ownership

I am in the process of learning how to do system administration (just on my own Linux machine) and have been working with the find command. One of the things I tried was find / -nouser -o -nogroup I redirected the output of my find query into a text file, and when I did a wc -l on it, it... (1 Reply)
Discussion started by: kermit
1 Replies

7. Shell Programming and Scripting

getting rid of duplicate files

i have a bad problem with multiple occurances of the same file in different directories.. how this happened i am not sure! but I know that i can use awk to scan multiple directory trees to find an occurance of the same file... some of these files differ somwhat but that does not matter! the... (4 Replies)
Discussion started by: moxxx68
4 Replies

8. UNIX for Dummies Questions & Answers

Renaming multiple files, to get rid of extension

I have a good script to rename multiple files, but what's the best way I can remove some text from multiple filenames? Say I have a directory with 35 files with a .XLS at the end, how can I rename them to remove the .XLS but keep everything the same, without having to mv manually. Thanks. (6 Replies)
Discussion started by: nj78
6 Replies

9. Solaris

getting rid of ^H

Hello everybody I have a very annoying problem on my Solaris (Unix in general) servers. When I open a shell and press the backspace button, it results in a ^H character being printed on screen. I can resolve it by typing stty erase <backspace>, but does anyone know how I can prevent the... (3 Replies)
Discussion started by: soliberus
3 Replies

10. UNIX for Advanced & Expert Users

How to get rid of DCHP

Hi all:- I am using Sun8 Intel PC in a network and have IP allocated by DHCP. Now I got a static IP and want to configure my PC for this new IP and disable DHCP. How can I do that ? Thanks in advance (6 Replies)
Discussion started by: s_aamir
6 Replies
Login or Register to Ask a Question