Visit Our UNIX and Linux User Community


Mv all files with different extensions to a new name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mv all files with different extensions to a new name
# 1  
Old 10-24-2016
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

Code:
% mv foo.* bar_!#:1

is not working.

Thank you for your help


marek
# 2  
Old 10-24-2016
Code:
for i in `ls`; do
  mv $i bar_$i
done

# 3  
Old 10-24-2016
Thank you Padow1

This is only working in bash. And unfortunately its working for ever file name.

Could it be, that you meant:

Code:
for i in `ls foo.*`; do
  mv $i bar_$i
done

Is this possible in tcsh too?
# 4  
Old 10-24-2016
Hi,

Can you try the following ?
Code:
foreach i (foo.*)
echo "mv $i bar_$i"
end

You can remove echo and double quotes if that is what you wanted.

@Padow1 : for is not supported in tcsh. Also, ls is not required in for loop.

Last edited by greet_sed; 10-24-2016 at 03:44 PM.. Reason: update text
# 5  
Old 10-25-2016
Thank you greet_sed!

I tried really everything. My tcsh is: tcsh 6.19.00 (Astron) 2015-05-21 (unknown-unknown-bsd44) options wide,nls,dl,al,kan,sm,rh,color,filec

(I am on Mac)

Code:
foreach i `ls foo.*`\
? echo "mv $i bar_$i"\
? end
i: Undefined variable.

foreach i `ls foo.*`\
? echo `mv $i bar_$i`\
? end
foreach: Words not parenthesized.

foreach i (`ls foo.*`)\
? echo `mv $i bar_$i`\
? end
foreach: Words not parenthesized.

foreach i \(`ls foo.*`\)\
? echo `mv $i bar_$i`\
? end
foreach: Words not parenthesized.

foreach i \(`ls foo.*`\)\
? echo "mv $i bar_$i"\
? end
i: Undefined variable.

foreach i \(foo.*\)\
? echo "mv $i bar_$i"\
? end
i: Undefined variable.

Best greetings!

marek
# 6  
Old 10-25-2016
Hi,

You did not try what i have posted in post#4. Here is the tested code.

Code:
touch foo1 foo2 foo3
ls
foo1_bar  foo2_bar  foo3_bar
foreach i ( foo* )
foreach? mv $i "$i"_bar
foreach? end

ls
foo1_bar  foo2_bar  foo3_bar

Code:
tcsh --version
tcsh 6.15.00 (Astron) options wide,nls,lf,dl,al,kan,sm,color,filec

This User Gave Thanks to greet_sed For This Post:
# 7  
Old 10-25-2016
Hi.

Here's a script, but it should work interactively:
Code:
#!/usr/bin/env tcsh

# @(#) s1       Demonstrate rename utility.

# Infrastructure details, environment, commands for forum posts. 
# Uncomment setenv command to run script as external user.
# setenv PATH "/usr/local/bin:/usr/bin:/bin"
echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1 && version '=o' tcsh"
echo

touch foo.aux foo.log foo.pdf foo.tex
rm -f bar_foo.aux bar_foo.log bar_foo.pdf bar_foo.tex
echo " Desired result:"
echo bar_foo.aux bar_foo.log bar_foo.pdf bar_foo.tex

echo
echo " Files before operation:"
ls foo* bar*

rename 's/^/bar_/' foo*

echo
echo " Files after  operation:"
ls foo* bar*

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
tcsh 6.18.01

 Desired result:
bar_foo.aux bar_foo.log bar_foo.pdf bar_foo.tex

 Files before operation:
foo.aux  foo.log  foo.pdf  foo.tex

 Files after  operation:
bar_foo.aux  bar_foo.log  bar_foo.pdf  bar_foo.tex

I assumed a typo omitting the bar_foo.log filename:
Code:
foo.aux foo.log foo.pdf foo.tex to bar_foo.aux bar_foo.pdf bar_foo.tex

Other solutions for renaming files:
Code:
Rename multiple files, groups of files

        1) rename -- Debian version and RedHat version differ, q.v.
           (try package util-linux:
           http://en.wikipedia.org/wiki/Util-linux)

        2) ren -- RedHat relatives

        3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname

        4) mved -- (circa 2006; good as of 2015.05), perl
           http://raf.org/mved/
           (An earlier shell version may be available.)

        5) rename -- perl builtin library routine (DIY)

        6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns

        7) gprename - batch rename using a GUI

        8) krename - batch rename using a GUI

As usual, the common advice is to avoid csh-family for scripting in favor of Bourne-family scripting (sh, bash, ksh, zsh)

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #206
Difficulty: Easy
Exterior Gateway Protocol (EGP) is a modern, widely used, routing protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files with different extensions and same name

Hi, i have two folders. Folder A has 1000+ files with just images named like : 01012015.png 01022015.png etc. Folder B has much more files, part of them with same name as png files folder others not. Instead of folder A wich are only images, these are audio wav files. I need to... (11 Replies)
Discussion started by: Board27
11 Replies

2. UNIX for Dummies Questions & Answers

How to tar files of different extensions in one command

Linux RHEL 5.4 It is easy to create a tarball when you have files same extension For eg: You want to tar all files with the extension .log . This is easy tar -cvf diagnose.tar *.log I have two files with different extensions .log and .sh : error.log myscript.sh I want to create a... (5 Replies)
Discussion started by: John K
5 Replies

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

4. Shell Programming and Scripting

Searching for files with specific extensions

Hi, Could someone give me a hand with a search for files with two possible extensions, please. The requirement is simple - I need to issue a single ls command searching for files with the suffix of, say, *.txt and *.log. I've tried to use ls *.txt *.log which works if there are both... (4 Replies)
Discussion started by: neilmw
4 Replies

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

6. UNIX for Dummies Questions & Answers

changing extensions for multiple files at once

I copied some files to another folder, and I want to change them from .doc extensions to .txt extensions. I tried using the cp and mv commands, but it didn't work. Is it possible to change file extensions with these commands, and if so how do I do it? I tried using the * wildcard (say cp *.doc... (1 Reply)
Discussion started by: Straitsfan
1 Replies

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

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

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

10. UNIX for Dummies Questions & Answers

Command to select files with different extensions

I want to select files which have different extensions such as .cpp, .cs, .h I can select one of them as find . -name "*.cpp" but I want to select all of them in one command only. It should be pretty simple but I'm not able to get it. Any help with the command will be greatly appreciated. (1 Reply)
Discussion started by: MobileUser
1 Replies

Featured Tech Videos