File renaming Linux
Asked Answered
C

5

9

I have tried to rename several files on my Linux system. I usedrename 's/foo/bar/g' * All the files that I wish to change are in the current directory. It does not change the name of the files but I think it should. Any help would be appreciated.

Cusco answered 16/8, 2015 at 18:46 Comment(9)
Try rename foo bar * or just read the manpage.Annoying
do you have enough permissions to rename the files?Dumbfound
Have a look at rename files in directorySather
Erwin Yes I have permission to change the files.Cusco
hasufell The foo is just a string within the various names,Cusco
JGreenwell yes I looked at the <rename files in directory> and notice a similar code segment which I believe is perl. I thought s/ / /g was Bash.Cusco
hasufell Yes your code worked. I am trying to learn about how s/ / /g works and why it didn't for me.Cusco
yes, rename is a perl5 function that can be linked toinstead of the built-in rename: to learn how the regex substr works in perl 5 check out perlop and perlre or in Perl 6.Sather
JGreenwell Thank you. I will check out your referenced articles.Cusco
P
20

An easy way would to do:

mv file2rename newname
Phelps answered 17/8, 2015 at 0:52 Comment(2)
thanks for short but the most useful answer.Daffy
short and efficient, coolSisyphean
S
3

You have mentioned that you want to rename multiple files at once using rename expression. Technically you can't use only * sign for change file names. * means all files with same name. We know same file types doesn't exist with same name but you can rename some selected part from file. For an example

admin@home:~/works$ ls test*.c
test_car.c test_van.c test_dog.c

  • you can rename some part of these files not full name. because there cannot be exist same file name with same extention

admin@home:~/works$ rename 's/test/practice/' *.c

  • After executing this command every test replace with practice.

admin@home:~/works$ ls practice*.c
practice_car.c practice_van.c practice_dog.c

Straighten answered 30/11, 2019 at 17:1 Comment(0)
S
3

Rename a file mv

 mv old_name new_name

The use of the mv command changes the name of the file from old_name to new_name.

Snubnosed answered 27/1, 2020 at 8:10 Comment(0)
S
1

Another way to rename file extentions in the current directory, for instance renaming all .txt files in .csv:

for file in $(ls .); do
    mv $file ${file/.txt/.csv}
done

This will not affect files that don't have the .txt extention and it will prompt an error (should be developed further depending on your needs).

Spire answered 27/1, 2020 at 8:22 Comment(0)
A
0

some posts points out the usage of for x in $(something); do..

please - Don't (ever, under any circumstances) use that! (see below)

Say you have a file(and, other .txt files):

"my file with a very long file - name-.txt"

and you do for f in $(ls *.txt); do echo $f; done (or something like that) it will output

.
..
a.sh
docs
my
file
with
a
very
long
file
-
name-.txt

(or something similar)

Instead, try the following:


#! /bin/sh

if [ $# -eq 0 ]; then
    echo -n "example usage: bash $0 .txt .csv <DIR>"
    echo -n "(renames all files(ending with .txt to .csv) in DIR"
    exit
fi

A="$1"      # OLD PREFIX (e.g .txt )
B="$2"      # NEW PREFIX (e.g .csv )
DIR="$3*$A" # DIR     (e.g ./   )

# for file f;
# in path  $DIR;
for f in $DIR; do
  ## do the following:
  # || here just means:
  #         only continue IFF(if and only if)
  #         the previous is-file-check's exit-status returns non-zero
  [ -e "$f" ] || continue
  
  # move file "$f" and rename it's ending $A with $B (e.g ".txt" to ".csv")
  # (still, in $DIR)
  mv "$f" "${f/$A/$B}"
done

###    $ tree docs/

#    docs/
#    ├── a.txt
#    ├── b.txt
#    ├── c.txt
#    └── d.txt
#
#    0 directories, 4 files
#


###   $ bash try3.sh .txt .csv docs/


#    $ tree docs/
#    docs/
#    ├── a.csv
#    ├── b.csv
#    ├── c.csv
#    └── d.csv
#
#    0 directories, 4 files
##
#-------------------#

References:

 MAN's: ($ man  "- the following")
- bash
- mv
- ls

Note: I do not mean to be offensive - so please don't take it as offense (I got the main command-idea from meniluca actually!

But since it was (for x in $(ls ..)) I decided to create a whole script, rather than just edit.

Autoeroticism answered 22/8, 2021 at 12:44 Comment(1)
If this can be improved, please feel free to point it out!Autoeroticism

© 2022 - 2024 — McMap. All rights reserved.