File Extension Substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Extension Substitution
# 8  
Old 05-26-2016
Quote:
Originally Posted by RudiC
then /home/config.* will do what you need.

/home/Config.{xml,xml.bak} is a construct that recent shells (like bash, which you may or may not use) will expand to /home/Config.xml /home/Config.xml.bak - exactly what * with above conditions would yield.
Yes, and no.
Code:
file=/home/Config.*

will set file to /home/Config.* (not /home/Config.txt and not /home/Config.xml).

After setting file=/home/Config.*, [ -f "$file" ] won't match either file, but [ -f $file ] (without quotes) would be OK until /home/Config.txt.bak or /home/Config.xml.bak is created and then you'd get syntax errors from the test command when the expansion of the unquoted $file expands to two filenames. And, the common commands to create a backup file:
Code:
cp $file $file.bak

and:
Code:
cp $file "$file.bak"

would create a file named Config.*.bak; not Config.txt.bak and not Config.xml.bak. And, the more commonly used:
Code:
cp "$file" "$file.bak"

would fail with a file not found error.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 05-26-2016
True, but wouldn't the restrictions apply for the {...,...} construct, too? It explains the error the user described in post#1.
I was thinking more when the shell expands the construct, like in echo ... or ls ..., so the assignment to a variable would need a command substitution like Don Cragun did in post#6, like file=$(echo /home/config.*)
# 10  
Old 05-26-2016
Quote:
Originally Posted by RudiC
True, but wouldn't the restrictions apply for the {...,...} construct, too? It explains the error the user described in post#1.
I was thinking more when the shell expands the construct, like in echo ... or ls ..., so the assignment to a variable would need a command substitution like Don Cragun did in post#6, like file=$(echo /home/config.*)
Yes, it applies to file.{xml,txt} just as much as it does to file.*, file.[tx][xm][tl], or file.???.

The diagnostic shown in post #1 in this thread could be because pathname expansion does not occur during assignments (true in all shells as far as I know) or it could be because the shell the user is using does not recognize the syntax {element[,element]...} as a construct to be processed during pathname expansion (this syntax is an extension that is not defined by the standards). Since that post did not specify what shell is being used and did not show the command using $file (and whether or not it was enclosed in double quotes), we don't have enough information to know if:
Code:
file=$(echo /home/config.{xml,txt})

would work in that user's environment. All three of the suggestions I made in post #6 will work with any POSIX-conforming shell under the conditions specified by the OP (i.e., one and only one of the two desired files is present).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

File Extension Missing in file-$var.csv

I'm afraid this is a silly question but I can't figure it out. I have a script like so... echo "Enter DRDL Signature Version Number" read DRDL_Number mv signature_output.csv SERVICE_OBJECTS_S-$DRDL_Number.csv The resultant filename does not contain the .csv as follows.... (3 Replies)
Discussion started by: Cludgie
3 Replies

3. 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

4. Shell Programming and Scripting

Getting the file extension

I have a file n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod and I want to get the extension. At the moment I have set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'` which of course does not work as there is a point in varp0.25 (13 Replies)
Discussion started by: kristinu
13 Replies

5. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

6. UNIX for Dummies Questions & Answers

Changing file extension

Hello all, I need to change file extension for all .doc files to .txt file in multiple folders. I know the way to rename them by going to each folder and doing that, but I need something which I can run from home directory so that It does the renaming in all the nested directories. Thanks. (4 Replies)
Discussion started by: jaysean
4 Replies

7. Shell Programming and Scripting

bad substitution Error while renaming Extension

Hi All, We are in the process of Migrating from AIX 4 to Solaris 10 and getting a Few Errors. I have been programming in shell but could never establish muself as an expert, hence please need you help. I am Getting Bad Substitution error in my script, I have isolated the issue and its... (6 Replies)
Discussion started by: paragkhanore
6 Replies

8. UNIX for Dummies Questions & Answers

About the file extension

Hi everyone, Can we know that which type of file is there without opening a file thanks in advance.... kunal patil (3 Replies)
Discussion started by: kunalpatil09
3 Replies

9. UNIX for Dummies Questions & Answers

changing file extension

Hello, everyone! :] I'm having an issue with my camera/uploading to Photobucket. When my camera transfers it's photos to my hard drive, it transfers them as .JPG. Unfortunately, when I go to upload to my Photobucket account, it only accepts .jpg & .jpeg files. Every time I want to upload, I... (6 Replies)
Discussion started by: spidydude
6 Replies

10. Shell Programming and Scripting

rename file extension

I am trying for loop to rename file extension from .txt to .html : as below : for i in *.txt; do mv "$i" `basename $i`.html; done ------------------------------------------- But this renames a file file1.txt as file1.txt.html anyone know how get avoid .html added after .txt ? it... (4 Replies)
Discussion started by: sriram003
4 Replies
Login or Register to Ask a Question