Rename the files by using code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the files by using code
# 8  
Old 04-05-2012
And another one:

Code:
for f in file*.dat; do
  _f=${f%.dat}
  mv -- "$f" "${_f/file/file-}"
done

The /pattern/substitution/ parameter expansion
is not standard/POSIX (it's supported by most modern shells though: zsh, ksh93 and bash).

This should be standard enough:

Code:
for f in file*.dat; do
  _f=${f#file}   
  mv -- "$f" file-${_f%.dat} 
done

Both solutions assume that the leading part is a known constant.

With zsh:

Code:
zsh-4.3.14[t]% autoload -U zmv
zsh-4.3.14[t]% rm file*
zsh-4.3.14[t]% touch file356.dat file358.dat file1521.dat
zsh-4.3.14[t]% zmv -v '(file)(<->).dat' '$1-$2' 
mv -- file1521.dat file-1521
mv -- file356.dat file-356
mv -- file358.dat file-358


Last edited by radoulov; 04-05-2012 at 06:39 PM..
This User Gave Thanks to radoulov For This Post:
# 9  
Old 04-06-2012
why don't use the shell builtin feature
Code:
for FILE in file*; do
     mv ${FILE} file-${FILE//[^0-9]/}
done

These 2 Users Gave Thanks to complex.invoke For This Post:
# 10  
Old 04-06-2012
Quote:
Originally Posted by huaihaizi3
why don't use the shell builtin feature
Code:
for FILE in file*; do
     mv ${FILE} file-${FILE//[^0-9]/}
done

Yep, good catch!
# 11  
Old 04-06-2012
Quote:
Originally Posted by huaihaizi3
why don't use the shell builtin feature
Code:
for FILE in file*; do
     mv ${FILE} file-${FILE//[^0-9]/}
done

OK, but note that this is also bash/ksh93 only, not POSIX. For a general solution you would need to double quote the file names otherwise it will break on file names with spaces or unusual characters..

Code:
for FILE in file*; do
  mv "${FILE}" "file-${FILE//[^0-9]/}"
done

This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 04-06-2012
Quote:
Originally Posted by Scrutinizer
OK, but note that this is also bash/ksh93 only, not POSIX.
... and zsh.

Quote:
For a general solution you would need to double quote the file names otherwise it will break on file names with spaces or unusual characters..

Code:
for FILE in file*; do
  mv "${FILE}" "file-${FILE//[^0-9]/}"
done

Good point! And if we want to generalize it further, we can add -- between
the command and the filenames passed as arguments in order
to avoid problems with filenames that begin with -.
# 13  
Old 04-06-2012
Quote:
Originally Posted by radoulov
... and zsh.

Good point! And if we want to generalize it further, we can add -- between
the command and the filenames passed as arguments in order
to avoid problems with filenames that begin with -.
Also a good point! An alternative would be to use a path name
Code:
mv "./${FILE}" ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

3. UNIX for Dummies Questions & Answers

Rename Files

Hi all, I am newbie and Im trying to rename a set of files & there are over 2900 of them. So, the best way I thought was through a script and here is what I got & it doesnt work. Im not sure as how to figure this out. Thanks Gonzalez Here is what I have - -a:~/Users/GonzaPue/ls -altr... (3 Replies)
Discussion started by: PG3
3 Replies

4. UNIX for Dummies Questions & Answers

Rename all .sh files to .pl

I have various .sh and .pl files in one directory. I want to rename all the .sh files to .pl i.e testscript.sh --> testscript.pl I am trying to use mv *.sh *.pl It doesnt work though!! (3 Replies)
Discussion started by: chrisjones
3 Replies

5. Shell Programming and Scripting

Rename files

Hi, I am new to Unix and i have a requirement where i need to write a shell script where i have to loop through various files present in a directory and rename them based on below criteria. Files in the folder are in the following format. _YYYYMMDD.dat] SDL_V1_20100530.dat... (6 Replies)
Discussion started by: bishoo
6 Replies

6. Shell Programming and Scripting

Rename many files

Hi, I have many files ex: file1, file2 ...file100, and I would like to rename only files with "1" in name. I don't have experience with bash and other shells. I know I can use "for i in" and "if", and I can use "sed" to change "1" but I have no idea how should "if" look. I will be grateful... (6 Replies)
Discussion started by: Physix
6 Replies

7. Shell Programming and Scripting

Rename files

I wrote a script that accepts filenames as argument but I am having difficulty if filename has both(uper/lower) cases..so I want to write a script that accepts one or more filenames as arguments and converts filenames to uppercase..(actually rename files..) (2 Replies)
Discussion started by: aadi_uni
2 Replies

8. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

9. Shell Programming and Scripting

Rename many files

Hi all I have files in the following format: 01_anote1.pdf 01_bnote1.pdf 01_control1.pdf 01_ethics1.pdf 01_invoice1.pdf 01_invoice_21.pdf 20_quote_l1.pdf I need to rename them to 01_anote.pdf 01_bnote.pdf 01_control.pdf 01_ethics.pdf 01_invoice.pdf (9 Replies)
Discussion started by: lmatlebyane
9 Replies

10. UNIX for Dummies Questions & Answers

How to rename files?

:confused: How can i rename a file 'x.log' to 'x_20020512 072909.log' :eek: i'm using perl, with system command from a unix web server, and need to timestamp my logs if the above format (filename _ year month day hr min sec .log) (9 Replies)
Discussion started by: CompuTelSystem
9 Replies
Login or Register to Ask a Question