Removal of file extension question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removal of file extension question
# 1  
Old 03-22-2011
Removal of file extension question

All,
I know that this will remove a file extension from a file name, but reading on the documentation on how it works confuses me.
Code:
${filename%.*}

Can anyone explain what exactly is going on here? Filename is the pattern and % says to cut anything that starts with .?

Also, can I run this command and then insert a timestamp in between the filename and the extension? I am hoping to do something like mv $filename ${filename%.*}`date`.txt. Can this be done without a for loop for each file with the exec command?
Code:
find $DESTLOC -maxdepth 1 -type f -exec mv -f {} $ARCHLOC/


Last edited by markdjones82; 03-22-2011 at 12:22 PM..
# 2  
Old 03-22-2011
The keyword should be a shell's parameter expansion.

Code:
% cut out shortest suffix (right side)
%% cut out longest suffix (right side)
# cut out shortest prefix (left side)
## cut out shortest prefix (left side)

#### EXAMPLE (the slash is just the delimeter) ####

$> VAR=eins/zwei/drei/vier
$> echo ${VAR%/*}
eins/zwei/drei
$> echo ${VAR%%/*}
eins
$> echo ${VAR#*/}
zwei/drei/vier
$> echo ${VAR##*/}
vier

This book is a good choice regarding such things and many other helpful concepts etc.:
Pro Bash Programming: Scripting the Linux Shell - Links
# 3  
Old 03-22-2011
Quote:
Originally Posted by zaxxon
The keyword should be a shell's parameter expansion.

Code:
% cut out shortest suffix (right side)
%% cut out longest suffix (right side)
# cut out shortest prefix (left side)
## cut out shortest prefix (left side)

#### EXAMPLE (the slash is just the delimeter) ####

$> VAR=eins/zwei/drei/vier
$> echo ${VAR%/*}
eins/zwei/drei
$> echo ${VAR%%/*}
eins
$> echo ${VAR#*/}
zwei/drei/vier
$> echo ${VAR##*/}
vier

This book is a good choice regarding such things and many other helpful concepts etc.:
Pro Bash Programming: Scripting the Linux Shell - Links
Thanks, I guess "expansion" is what confused me as I don't equate expansion with removal. So really, you aren't expanding, but deleting.
# 4  
Old 03-22-2011
You can delete the filename extensions easily by doing:
Code:
for i in *.txt; do mv $i ${i%%.*}; done

If you want to put some file extensions back to files, copy the files into their own directory (for precaution) and put file extensions on them like this (.txt in this case):
Code:
for i in *; do mv $i $i.txt; done


Last edited by Franklin52; 03-23-2011 at 04:58 AM.. Reason: Please use code tags, thank you
# 5  
Old 03-22-2011
Quote:
Originally Posted by sgruenwald
You can delete the filename extensions easily by doing:

for i in *.txt; do mv $i ${i%%.*}; done

If you want to put some file extensions back to files, copy the files into their own directory (for precaution) and put file extensions on them like this (.txt in this case):

for i in *; do mv $i $i.txt; done
Thanks, I figured I could do it with the for loop I was just wondering if I could some how do it with the find -exec mv command
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

2. Shell Programming and Scripting

String removal from file

Dear all From below mention input file I needed op file as show below. I am using below code but not worked. I/p file BSCBCH1 EXAL-1-4 WO* SMPS MAINS FAIL BSCBCH1 EXAL-1-5 WO* SMPS RECTIFIER FAIL BSCBCH1 EXAL-1-6 WO* SMPS MAJOR ALARM BSCBCH2 EXAL-1-10 WO* ... (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

3. Solaris

Removal of zip file permanently

Hi Everyone, I see some peculier thing happening on my server. I have one zipped file created long back as a normal user and trying to remove it now. When i tried to remove as that particular user, i was not able to do that. So i logged in as a root user and removed that successfully. But it... (8 Replies)
Discussion started by: Sricharan21
8 Replies

4. Shell Programming and Scripting

Removal of HTML ASCII Codes from file

Hi all, I have a file with extended ASCII codes in the description which needs to be removed. List of extended ascii codes "Œ", "œ", "Š", "š", "Ÿ", "ƒ", "-", "-", "‘", "'", "‚", "“", "”", "„","†", "‡", "•", "...", "‰", "€", "™" Sample data: Test Details-HAVE BEEN PUBLISHED... (1 Reply)
Discussion started by: btt3165
1 Replies

5. Shell Programming and Scripting

Help with removal of numericals in a file

I have a file from which I want to eliminate the numerical values.. The contents of the file are as shown below.. 1 a1,b,2 1,b,c 2 a2,4,b a,b,2 From the above file I want eliminate only the numberical values(except the line numbers which are at the beginning).. The file... (12 Replies)
Discussion started by: abk07
12 Replies

6. Shell Programming and Scripting

Help with removal of blank spaces in a file

Hello.. I have a text file. I want to remove all the blank spaces(except tab) from the file.. I tried using sed command as shown below sed 's/ //g' file1 But the problem with the above command is that it also eliminates 'tab' which is between the columns.. For example if the contents... (7 Replies)
Discussion started by: abk07
7 Replies

7. Shell Programming and Scripting

Removal of carriage returns from a comma delimited file

Hi, I have a file which is having some carriage return in one of the field for which single line is coming in multiple lines. I want to combine all those multiple lines of that field into one line. Eg: Input: Id, Name, Location, Comments, Dept 2, John, US, I am from US. I... (5 Replies)
Discussion started by: mahish20
5 Replies

8. Shell Programming and Scripting

Removal of Duplicate Entries from the file

I have a file which consists of 1000 entries. Out of 1000 entries i have 500 Duplicate Entires. I want to remove the first Duplicate Entry (i,e entire Line) in the File. The example of the File is shown below: 8244100010143276|MARISOL CARO||MORALES|HSD768|CARR 430 KM 1.7 ... (1 Reply)
Discussion started by: ravi_rn
1 Replies

9. UNIX for Dummies Questions & Answers

Job Watcher Removal Question

All- What is the command to remove/delete a job watcher from production. Thanks, Sailor58 (2 Replies)
Discussion started by: Sailor58
2 Replies

10. Solaris

UNIX File removal without conformation

I need to remove permanently some 3 GB of temp folder which contains Log file, simulation files from my disk. if i use "rm - rf <filename>" command it asks about conformation for accessing each folder and for removing every file and giving yes to every message in terminal window is very... (2 Replies)
Discussion started by: rajharvijay
2 Replies
Login or Register to Ask a Question