file renaming with if else clause


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file renaming with if else clause
# 1  
Old 11-25-2011
file renaming with if else clause

Hi.
I am trying to rename some files with an if else clause.

So far I am doing it this way:

for file in *;
do mv $file `echo $file|sed 's/$/.txt/'`;
done

This make all my files have a .txt at the end of their name.
But since I had some .jpg files in my folder they end up being .jpg.txt.
So now I am stripping just the jpg files from their txt end:

>for file in *.jpg.txt; do mv $file `echo $file|sed 's/.jpg.txt/.jpg/'`; done

How could I do this in awk or shell scripting with an if else clause:

I would like my script to be in this format:

if (my file ends on jpg don't do anything)
else (add jpg to my file)

Thankful for any help!
# 2  
Old 11-25-2011
Maybe this:
Code:
find ./ \! -name "*.jpg" -type f | \
while read fname
do
     mv "${fname}" "${fname}.txt"
done

Or even better:
Code:
find ./ \! -name "*.jpg" -type f | xargs -t -I {} mv "{}" "{}.txt"

If your find supports "-printf" option:
Code:
# List all commands
find ./ \! -name "*.jpg" -type f -printf "mv %h/%f %h/%f.txt\n"
# Execute the commands
find ./ \! -name "*.jpg" -type f -printf "mv %h/%f %h/%f.txt\n" | sh


Last edited by felipe.vinturin; 11-25-2011 at 09:55 AM..
# 3  
Old 11-25-2011
I think I am starting to understand what you did:
find ./ \!-name "*.jpg" -type f

Find in this directory all files that are not jpg files! Correct?
How would I make this statement for 2 files at once? Jpg and png?
Later on you are creating a variable called fname? Does this name come from
-name -type f: fname ???

Last: How would you go about the opposite: Strip all files of the jpg:
find ./ -name "*.jpg" -type f

do mv "${fname}" "${fname}" ??? How do you say you want to get rid of the jpg...?

Thank you
# 4  
Old 11-25-2011
You are correct! The "\!" negates the "-name" option, so, it means: not -name "*.jpg"

About your other questions:
Code:
# 1. The "read fname" puts the result of the find command in the variable fname.
# Try: man read

# 2. In the same find command you can negate jpg and png
find ./ \( \! -name "*.jpg" -a \! -name "*.png" \) -type f

# For further, find man pages are really good!

# 3. Removes the .jpg
find ./ -name "*.jpg" -type f | \
while read fname
do
     # The sed below removes the ".jpg" from the contents of the variable: fname
     nFName=`echo "${fname}" | sed 's#\.jpg##'`
     mv "${fname}" "${nFName}"
done

# 5  
Old 11-25-2011
Code:
for file in *
do
  case $file in
    *.jpg) ;; ## do nothing
    *) mv "$file" "$file.txt" ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oracle 11g script read from file in where clause (RHEL 5.7)

Hi everyone, Simple question. I have a comma-delimited file, aux.txt, with the following contents (all in one line): 'value1','value2','value3',...,'valueN' I would like to know if there's a way I can use that file inside a sql script in a where clause, like so: select myfield1 from mytable... (6 Replies)
Discussion started by: gacanepa
6 Replies

2. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

3. UNIX for Dummies Questions & Answers

Need help with Renaming a file

I have a file named as Pro_PatAct_MMDDYYYY.csv. I need to renmae it to this Pro_PatAct.csv without the date timestamp. Can someone help me to achieve this using a regular expn. (3 Replies)
Discussion started by: imran_affu
3 Replies

4. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

5. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

6. Shell Programming and Scripting

Bash-Shell: If-Clause to check if file is empty

Hello, I want to checkl whether my file has text in it or not. if ; then ... if ; then ... But none of these work Can someone help me? ---------- Post updated at 09:00 AM ---------- Previous update was at 08:55 AM ---------- The code-tags caused an displayerror,... (5 Replies)
Discussion started by: ABE2202
5 Replies

7. Shell Programming and Scripting

Renaming a file

Hai masters, Suppose i have so many files named xxxx.ksh.tar, yyyy.ksh.tar and so on. My requirement : i want to rename all the files with extension ".ksh.tar" to ".tar" in my directory. Could you please tell me the solution for this.. (5 Replies)
Discussion started by: ecearund
5 Replies

8. Shell Programming and Scripting

file renaming

How can I rename files named like these iq - 000001 - 2008.07.31 - 14.49.47 - location1.bin iq - 000001 - 2008.07.31 - 14.49.47 - location12.bin iq - 000008 - 2008.07.31 - 14.52.01 - location500.bin to iq_2008.07.31_14.49.47_location1.bin iq_2008.07.31_14.49.47_location12.bin... (7 Replies)
Discussion started by: larne
7 Replies

9. UNIX for Dummies Questions & Answers

if clause

hi, pls could you help me with one program in KSH ( i have sunOS). I need to create an If clause, that prints an error message and filenames, when in a directory are found some files of null size (find . -type f -size 0 ). thanks (3 Replies)
Discussion started by: palmer18
3 Replies

10. Shell Programming and Scripting

Renaming a file name

I have an audit log that is produced each day from a production printer. It names the file using todays date, but it removes the leading zero's. For example: todays date 060104 names the file 6104.txt. I ftp this file onto a Sun box and pull stats off of it. To keep some consistency to what I am... (2 Replies)
Discussion started by: dbrundrett
2 Replies
Login or Register to Ask a Question