script that looks for .old extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script that looks for .old extension
# 1  
Old 11-21-2010
script that looks for .old extension

Hey guys I have to make a script that takes one argument, the name of a directory, and adds the extension ".old" to all visible files in the directory that don't already have it.

I made one that loops through the file names and uses a grep to see if the extension .old is there and then renames it like this:

Code:
for filename in $(ls "$1")
do
   echo $filename | grep '\.old$' > /dev/null
   if [ $? != 0 ]
      then mv $1/$filename $1/$filename.old
   fi
done

Now I have to Use a for loop to go through the filenames in the directory. But, only loop through the filenames that do not have a ".old" extension. Inside this loop I have to rename the file to the new name. Anyone have any idea how I would do this?
# 2  
Old 11-21-2010
Code:
 
find $1 -maxdepth 1 -type f   -not -name "*.old" -exec mv {} {}.old \;

Code:
 
find $1 -maxdepth 1 -type f -not -name "*.old" | xargs -i{} mv {} {}.old

This User Gave Thanks to honglus For This Post:
# 3  
Old 11-22-2010
Quote:
Originally Posted by honglus
Code:
 
find $1 -maxdepth 1 -type f   -not -name "*.old" -exec mv {} {}.old \;

Code:
 
find $1 -maxdepth 1 -type f -not -name "*.old" | xargs -i{} mv {} {}.old

does this go inside the for loop?
# 4  
Old 11-22-2010
could use -v option of grep command to exclude .old files.And those curly braces are important when you append any letter or word (here: .old)
Code:
for filename in $(ls * | grep -v '\.old$' 2> /dev/null)
do
	#echo $filename | grep -v '\.old$' 2> /dev/null
	
	mv "${filename}" "${filename}.old"
done

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 11-22-2010
Quote:
Originally Posted by Joey12
does this go inside the for loop?
No, You don't need loop. either one-liner can do the job.
This User Gave Thanks to honglus For This Post:
# 6  
Old 11-22-2010
Code:
for filename in "$1"/*
do
  if [ -f "$filename" ] && [ ! -e "$filename.old" ]; then
    mv "$filename" "$filename.old"
  fi 
done

This User Gave Thanks to Scrutinizer For This Post:
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

Finding file extension on C Shell script

I very new to C Shell. I am trying to do is read from Command line. Find the if the file is zip, .txt, symbloic link,pipe, unknow (if file is not zip, txt, sy....) here is what I what got so far. I am very stuck atm Please help me out : If the file is symblooc link what file is link to ... (12 Replies)
Discussion started by: madbull41
12 Replies

3. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example file1.new.new.new file2.new.new.new file3.new.new.new file4.new.new.new i want to write a shell script that rename all such file with only single extension like file1.new... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

4. Shell Programming and Scripting

perl script to remove the extension from its name

There are few files in my windows directory and I need a perl script to rename the files to its original names i.e., the last extension(.orig) needs to be removed programatically, files in directory data1.htm.orig data2.htm.orig data3.htm.orig to be renamed to data1.htm data2.htm... (4 Replies)
Discussion started by: giridhar276
4 Replies

5. Shell Programming and Scripting

Script to create EVIM template with SAS extension

I write lots of SAS programs and would like to create a script that allows me to have a template each time I create a new program file. Specs: I use EVIM for my editor. I run SAS in batch mode. We use RedHat 6. I don't use c shell. I want a script that will do the following: >... (3 Replies)
Discussion started by: starbecks
3 Replies

6. Shell Programming and Scripting

Script to add extension to filename

Hi all, I have a folder with a bunch of files in them, and I would like to add an extension (.mp3)to all these filenames. The folder has only files that I'd like .mp3 added to. It looks something like this: Intput: File1 File2 File3Output: File1.mp3 File2.mp3 File3.mp3Thanks in... (2 Replies)
Discussion started by: repiv
2 Replies

7. Shell Programming and Scripting

Csh script get file extension

Hi All I have a csh shell script which should check if a given file is a zip file as below: **************************************** #!/bin/csh -f if ]; then echo is a zip file else echo sorry not a zip file endif exit **************************************** ... (1 Reply)
Discussion started by: raj144
1 Replies

8. UNIX for Dummies Questions & Answers

script takes the whole filename instead of just extension

I am running my script from "/abc/" this path and it has no ".csv files" but has a ".txt" files namely temp1.txt My script goes as below, wherein it is suppose to find files with *.txt extension and *.csv extension in another path namely "/abc/xyz/": #!/bin/ksh PATH1="/abc/xyz/" value="*.csv... (1 Reply)
Discussion started by: wolverine999
1 Replies

9. Shell Programming and Scripting

shell script string extension

hey, im looking for a way of extending a string in shell script. for example i have two strings "." and "abcd", i need to extend the first string so that it is the same length as the second. so "." and "abcd" becomes "...." and "abcd", could someone shed light on how to do this ? thanks (4 Replies)
Discussion started by: vbm
4 Replies

10. UNIX for Dummies Questions & Answers

shell script: forbid extension

Rather new to unix, so please don't beat me! I'm trying to get a list of files into a variable that I can use throughout the rest of the script. The challenge is that I need to exclude a certain extension from the list, and I'm having trouble with it. For example: item_a item_a.exe... (3 Replies)
Discussion started by: Loriel
3 Replies
Login or Register to Ask a Question