robertdaleweir
robertdaleweir

Reputation: 137

File renaming Linux

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.

Upvotes: 11

Views: 21729

Answers (5)

joshua
joshua

Reputation: 1

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.

Upvotes: 0

sahil
sahil

Reputation: 41

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.

Upvotes: 4

meniluca
meniluca

Reputation: 306

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

Upvotes: 1

Kalana
Kalana

Reputation: 6143

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

Upvotes: 3

An easy way would to do:

mv file2rename newname

Upvotes: 25

Related Questions