Appending text to a number of similar filenames


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Appending text to a number of similar filenames
# 1  
Old 10-09-2007
Appending text to a number of similar filenames

Hi,

I was wondering if there was a way to append something to filenames based on a wildcard. For example, if I have the following files in a directory:

blah1
blah2
blah3
blah4
blah5

I want to rename these all to:

blah1.txt
blah2.txt
blah3.txt
blah4.txt
blah5.txt

Is there a way to select all the blah files (using say, 'blah*'?) and then appending the same text to the end of them? I was looking at the mv command, but I was unsure of how to take all the filenames after saying 'mv blah*' and append text to them.

Thanks,
Dan
# 2  
Old 10-09-2007
Code:
for file in blah* ; do
    mv $file ${file}.txt
done

# 3  
Old 10-09-2007
Wow, I never even thought of using a loop. That works perfectly!

Just one more quick question:

Is there a similar method that will allow me to remove certain text from a number of files? For example, let's say I want to remove the .txt from all the files.

I apologize if this should easily follow from the above example, but I'm pretty new to this type of manipulation.

Thanks a lot,
Dan
# 4  
Old 10-09-2007
You could use this in combination with the previous technique.
Code:
# file=abcdef.txt
# echo ${file%%.txt}
abcdef

or use the basename command

Code:
# basename $file .txt
abcdef

# 5  
Old 10-10-2007
Awesome! Thanks so much for the help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending date to UNIX Filenames

Hello, I have a file name in the below format and have to append the date as _$currdate. kchik_UK_lo.txt_$currdate. The above should be the format and I dont want to put entire filename as above in the code, but it should give me the output as the above filename.Can anyone please help... (7 Replies)
Discussion started by: harika03
7 Replies

2. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

3. Shell Programming and Scripting

appending data from similar files

I am familiar with scripting, but I am trying to see if there is an easy way to append files from similar files into one file. For example, if there is file1_20121201, file1_20121202, file1_20121203, file2_20121201, file2_20121202, file2_20121203 I want to be able to combine all the data from... (3 Replies)
Discussion started by: mrbean1975
3 Replies

4. Shell Programming and Scripting

Increasing a number and appending it to next line of a text file

Hi all, I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on.. like the text... (5 Replies)
Discussion started by: smarty86
5 Replies

5. UNIX Desktop Questions & Answers

Appending file extensions to filenames in bash scripts

Hi Suppose I have a variable called filename and it it contains the name of a file. I then would like to append an extension to that filename. The filename currently has no extensions. How do I do this? Thanks (11 Replies)
Discussion started by: ladyAnne
11 Replies

6. Shell Programming and Scripting

Deleting extra files with similar filenames

Hello, I have a large amount of files under a root directory, with several sub-directories, and many of these sub-directories have similar files with similar names. I need to clean this up. The filenames are of the format: /path/to/dir/subdir/file name.dat /path/to/dir/subdir/file name... (3 Replies)
Discussion started by: smpatil4
3 Replies

7. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

8. UNIX for Dummies Questions & Answers

appending variable number of files

In a particular path of a server I have number of files.The files are generated every date with a date_mth stap on this.There are different files for different clients. For example in /data1 path i have X_0416_Score Y_0416_Score Z_0417_Score X_0417_Score A_0417_Score If i will run the... (1 Reply)
Discussion started by: dr46014
1 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. Shell Programming and Scripting

changing filenames in a directory to a number within a loop

hey guys. i'm new to shell scripting but not new to programming. i want to write a script that will take all the files in the current directory that end with a particular filetype and change all their names to a number in order. so, it would take all the jpg files and sort them in alphabetical... (30 Replies)
Discussion started by: visitorQ
30 Replies
Login or Register to Ask a Question