To replace a filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To replace a filename
# 1  
Old 08-27-2014
To replace a filename

Hi

i want to replace date stamp filename to original file name. i want to remove the datestamp after .txt For eg:

Code:
file_name.txt_01-1002014

output:
file_name.txt

tried with below one but not able to achieve
Code:
mv file_name.txt_01-1002014`basename "file_name.txt_01-1002014 "`.txt

# 2  
Old 08-27-2014
mv file_name.txt_01-1002014 file_name.txt
# 3  
Old 08-27-2014
Hi,

I am having set of files in a directories which needs to replaced. with normal mv command i can achieve it for one file. For e.g. in my $DIR i am having below files:
cd $DIR
f1_name1.txt-01-01-1988
f2_name2.txt-01-02-1987
. .
..
..
output:
Code:
f1_name1.txt
f2_name2.txt

# 4  
Old 08-27-2014
Code:
for FILE in "$DIR"/*.txt[_-]*
do
        if [ -e "${FILE/.txt*/.txt}" ] 
        then
                echo "${FILE/.txt*/.txt} already exists, not renaming $FILE"
                continue
        fi

        echo mv "$FILE" "${FILE/.txt*/.txt}"
done

Remove the 'echo' before the 'mv' once you've tested and seen it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-27-2014
Or something like:
Code:
for FILE in ....... 
do
  mv $FILE ${FILE%_*}
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

2. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

3. Shell Programming and Scripting

Replace Filename and text inside of directory

I have a directory that has directories that contain Dir-20111114-xyz and I want to change them to Dir-20111121-xyz. Inside of Dir-20111114-xyz, I have a config.xml file that also contains the date that I need changed from 20111114 to 20111121 I have used sed to replace inside of file not... (4 Replies)
Discussion started by: icculus99
4 Replies

4. Shell Programming and Scripting

replace and add characters in filename

I have files named like ABAB09s099E1AAV1.pdf and ABAB09s099E2AAV1.pdf in a directory. I need to add _Lop in the end of the name, like ABAB09s099E1AAV1_Lop.pdf for all files. For files with E2 in the name I also have to replace 99 with 88 in the filename, that would be ABAB09s088E2AAV1_Lop.pdf ... (3 Replies)
Discussion started by: hakkar
3 Replies

5. UNIX for Dummies Questions & Answers

replace multiple patterns in a string/filename

This should be somewhat simple, but I need some help with this one. I have a bunch of files with tags on the end like so... Filename {tag1}.ext Filename2 {tag1} {tag2}.ext I want to hold in a variable just the filename with all the " {tag}" removed. The tag can be anything so I'm looking... (4 Replies)
Discussion started by: kerppz
4 Replies

6. Shell Programming and Scripting

Replace text with filename

Hi, I have a few thousand files, each contains the word "hello". Would it be possible to do a batch job that replaces the word hello with the filename? Thanks. (2 Replies)
Discussion started by: calrog
2 Replies

7. Shell Programming and Scripting

Replace a filename with full path using sed

hi, i need to replace a line a file with a new raw device location.. original file.. /opt/sybase/ASE1502/ASE-15_0/bin/dataserver \ -d/data/TST_AKS1/sybdevices/master.dat \ -e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \ -c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \... (2 Replies)
Discussion started by: aksaravanan
2 Replies

8. Shell Programming and Scripting

Parse and replace string in filename

Hi, I've a filename of this format: "XXX_XXX_TXT.TAR.AS". Need to change the name into this format: "XXX_XXX.TAR.AS". This file resides in a directory. I'm ok with using the find command to search and display it. Essentially I just need to replace the string "_TXT.TAR.AS" to ".TAR.AS". Is awk... (17 Replies)
Discussion started by: chengwei
17 Replies

9. UNIX for Dummies Questions & Answers

Replace all files with a certain filename with another file

I tried searching for this, but I might have used the wrong terms as I couldn't find answers to this question. I'm looking for a way to replace all files with a certain filename with another file within a specific directory including all of it's subdirectory using a shell-script. (2 Replies)
Discussion started by: Schmellsera
2 Replies

10. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question