Add file extensions to files EXCEPT the script that is issuing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add file extensions to files EXCEPT the script that is issuing
# 1  
Old 03-27-2014
RedHat Add file extensions to files EXCEPT the script that is issuing

Greetings all,

On a RedHat System - I am issuing a command from script.sh that will add a file extension to a listing of files in a directory. It works, but I need to script from having an extension added as well.

Here is what I have tried to no luck:

Code:
for file in `ls * | awk ' /\./{print $0}'`
do
mv ${file} ${file}.ARD
done

Code:
for f in $(find . -type f); do mv $f ${f}.ARD; done

ARD being the extension I need to append. but it keeps adding it to script.sh. So at the end of the day I have script.sh.ARD, file1.ARD, file2.ARD, etc.

Any suggestions?
# 2  
Old 03-27-2014
You could always save $0 as a variable and clean up to yourfile.sh only...
Code:
 var=$0

Then run your code and let it add .ARD at the end.

Finally mv /full/path/to/$var.ARD /full/path/to/$var , just a simple idea...
# 3  
Old 03-27-2014
A bit past my scripting skills but I will look into it. Can you provide an example of how I might accomplish this
# 4  
Old 03-27-2014
Why don't you put a
Code:
mv $0.ARD $0

after your loop?
Or, if that is a recent bash, try
Code:
shopt -s extglob
ls !($0)

# 5  
Old 03-27-2014
Demonstration code, OSX 10.7.5 default bash terminal...
Code:
#!/bin/bash
# no_ext.sh
# This script launched as ~/no_ext.sh
this_script=$0
echo "This script from my home drawer, $this_script..."
# Now simulate your addition of .ARD from your code...
mv ~/no_ext.sh ~/no_ext.sh.ARD
# Check .ARD exists and original file doesn't...
ls -l ~/no_ext.*
# All other files also have .ARD at the end so....
mv ~/no_ext.sh.ARD $this_script
# Check it is back again and .ARD has gone...
ls -l $this_script*
# Set your permissions IF required, in my case 755...
chmod 755 $this_script
exit 0

Results:-
Code:
Last login: Thu Mar 27 19:56:25 on ttys000
AMIGA:barrywalker~> chmod 755 no_ext.sh
AMIGA:barrywalker~> ~/no_ext.sh
This script from my home drawer, /Users/barrywalker/no_ext.sh...
-rwxr-xr-x  1 barrywalker  staff  518 27 Mar 20:25 /Users/barrywalker/no_ext.sh.ARD
-rwxr-xr-x  1 barrywalker  staff  518 27 Mar 20:25 /Users/barrywalker/no_ext.sh
AMIGA:barrywalker~> _

# 6  
Old 03-27-2014
The files don't technically have extensions they are named along the lines of

FILENAME.IDENTIFIER.IDENTIFIER.EPOCHSTAMP
# 7  
Old 03-28-2014
Where does .ARD come from then?

If all your filenames end with .EPOCHSTAMP then this a much different animal.
I am assuming ".EPOCHSTAMP" is an on the fly 10 digit number which WILL change every second.

As the first two numbers of the epoch should be 13xxxxxxxx then try something like...
Code:
mv your_shell_script.sh.13* your_shell_script.sh

(Off to work.)

EDIT:-
Or more simply, (as you apparantly only have one script that is causing problems)...
Code:
mv your_shell_script.sh.* your_shell_script.sh


Last edited by wisecracker; 03-28-2014 at 06:18 AM.. Reason: See above...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mv all files with different extensions to a new name

Hello all! I want to move several files foo.aux foo.log foo.pdf foo.tex to bar_foo.aux bar_foo.pdf bar_foo.tex I am on tcsh % mv foo.* bar_!#:1 is not working. Thank you for your help marek (11 Replies)
Discussion started by: marek
11 Replies

2. AIX

AIX : Find files ignoring certain file extensions

Hi All, I am scripting a program to find and archive files. There are certain file types that I do not want to archive. Below is the scenario. I have created a lookup file which has details on folders days and file extensions that needs to be ignored I have separated the individual into... (4 Replies)
Discussion started by: kavinmjr
4 Replies

3. Shell Programming and Scripting

adding file extensions to split output files

Hello, I've searched this forum and others for a solution to my problem but nothing seems just right, I'm hoping I can get some help (seems like this should be easy, and I apologize if I've missed something on the forum): I have several large .fastq DNA sequence files (~20million reads,... (2 Replies)
Discussion started by: ljk
2 Replies

4. Shell Programming and Scripting

looping through files with different extensions

Hi all, I am trying to make a for loop invoking files with different extensions (*.ugrd and *.vgrd) and I cant just make it work. Cant figure out how to load the files so as to use them in subsequent commands like the ones in this pseudo code. the files are arranged such that in one date for... (8 Replies)
Discussion started by: ida1215
8 Replies

5. Shell Programming and Scripting

rename file, add extensions

Hi Guys, I have files named myfileaa,myfileab,myfileac,myfilesad.... till myfileav. Now i needs to rename all these files to myfileaa.txt ,myfileab.txt,myfileac.txt. Please help me how to do the same. Thanks in advance..!!! (4 Replies)
Discussion started by: jaituteja
4 Replies

6. Shell Programming and Scripting

Shell script regex help: accept only 3 file extensions

This regex is supposed to accept files with extensions 270, 276, and "txt" only. Everything else should be discarded. This is what I have. I'll spare you the rest of the code. ext =".\$" #ext =".\$" #ext =".\$" #ext =".\$" for xfile in `ls $dir | grep "$ext" | xargs`; do... (9 Replies)
Discussion started by: grep01
9 Replies

7. Shell Programming and Scripting

Rename files extensions

Hello ! i have a few files like ... setup.001 setup.002 setup.003 setup.004 // to setup.095 and i would like to rename those files to ... setup.r01 setup.r02 setup.r03 setup.r04 // to setup.r95 (7 Replies)
Discussion started by: Putazo
7 Replies

8. Shell Programming and Scripting

Sorting Files according to their Extensions...

I am trying to write a Korne Shell Script wherein we have to sort files according to their extensions(for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and move them to their respective directories viz. sh, csh and ksh... I think,... (1 Reply)
Discussion started by: marconi
1 Replies

9. Shell Programming and Scripting

Find files with 3 different extensions

Hi all, From one directory I need to fetch only files of type *.xls,*.csv,*.txt. I tried the find . -name '*.txt,*.csv,*.xls' -print. But it throws me error. Please do help me on this. Thanks Mahalakshmi.A (11 Replies)
Discussion started by: mahalakshmi
11 Replies

10. UNIX for Dummies Questions & Answers

List Files by Extensions

I have a unix directory with 500 plus files . When I do a ls -lR I can see ALL the files here . How can I sort this by the files extensions ? I can't enter ls -lR *.ext1 *.ext2 *.ext3 etc in case I miss out some files . (2 Replies)
Discussion started by: newbienix
2 Replies
Login or Register to Ask a Question