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

for i in `ls`; do
  mv $i bar_$i
done

Thank you Padow1

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

Could it be, that you meant:

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

Is this possible in tcsh too?

Hi,

Can you try the following ?

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.

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)

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

Hi,

You did not try what i have posted in post#4. Here is the tested 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
tcsh --version
tcsh 6.15.00 (Astron) options wide,nls,lf,dl,al,kan,sm,color,filec
1 Like

Hi.

Here's a script, but it should work interactively:

#!/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:

$ ./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:

foo.aux foo.log foo.pdf foo.tex to bar_foo.aux bar_foo.pdf bar_foo.tex

Other solutions for renaming files:

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

1 Like

Thank you greet_sed and dry!

Great postings. I finally understood your answers! It's working :slight_smile:

I installed the command rename via homebrew. This is a great help too.

Yes, I should move to bash. But I am so accustomed to commands like:

cp file_w30.tex !#:1:s/_w30/_w31/

would this be possible in bash too?

Thank you again

marek

Hi,

Following one works in bash from terminal through bash history ( Not from the script ) :

cp file_w30.tex !#:1:s/_w30/_w31/

!# - Refers to current command line typed so far.
1 - Refers to word 1 ( it starts from 0 - cp , 1 - file_w30.tex )
s/// - substitute _w30 to _w31

you can try like this from bash script :

cp file_w{30,32}.tex
1 Like

Hi, marek.

 
READLINE
       This is the library that handles reading input when using  an  interac-
       tive shell, unless the --noediting option is given at shell invocation.
       Line editing is also used when using the -e option to the read builtin.
       By default, the line editing commands are similar to those of Emacs.  A
       vi-style line editing interface is also available.  Line editing can be
       enabled  at  any  time  using  the -o emacs or -o vi options to the set
       builtin (see SHELL BUILTIN COMMANDS below).  To turn off  line  editing
       after  the  shell  is running, use the +o emacs or +o vi options to the
       set builtin.
...
      yank-nth-arg (M-C-y)
              Insert  the  first argument to the previous command (usually the
              second word on the previous line) at point.  With an argument n,
              insert  the nth word from the previous command (the words in the
              previous command  begin  with  word  0).   A  negative  argument
              inserts the nth word from the end of the previous command.  Once
              the argument n is computed, the argument is extracted as if  the
              "!n" history expansion had been specified.

Excerpt from man bash , q.v., describing one (small) part of the emacs editing mode for previously entered commands in the history. This is a very general scheme for editing commands. The advantage is that it uses emacs-like commands, so once you know one, the other is similar. Personally, I use the vi mode.

Best wishes ... cheers, drl

1 Like

Thank you!

cp file_w{30,32}.tex

That's really amazing! Very elegant! I will definately switch to Bourne shell. Opening a new thread, could somebody help me to move my .tcshrc profile to .bashrc ? Or would this be to childish for this professional forum?

Thanks again

marek

There's nothing like childish questions. But - you can help avoid even the impression by showing a correct and professional approach: formulate your request carefully, show your own attempts and where and how they fail (including details, e.g. error messages), and how you started curing the failures (e.g. applying topics from man pages).

And, yes, START a NEW thread - don't continue this old and solved one.