0

I have a set of folders named

-> section 1, section 2, section 3, ... , section 12

How do I change them to read

-> video 1, video 2 ..., video 12 

on a linux platform?

I tried:

mv section* video* 

But no love

0stone0
  • 34,288
  • 4
  • 39
  • 64
DCR
  • 14,737
  • 12
  • 52
  • 115
  • may need list with order then mv with new name, https://stackoverflow.com/questions/3211595/renaming-files-in-a-folder-to-sequential-numbers if you don't have sequential folders, list into an array and remove the `section` from the string and concat video on it – Lawrence Cherone Apr 10 '21 at 22:46
  • how? I do have sequential folders. – DCR Apr 10 '21 at 22:54
  • 1
    You could use `find` to *find* each file you want to rename. Then using any linux/bash tool to replace `section` with `video`. – 0stone0 Apr 10 '21 at 23:13
  • Thanks but these comments aren't really helpful. The problem is I have a space followed by a number. I want to keep the space and the number and just change the word section to video. A sample of how to do this would be really helpful. (I've been using linux for 3 days) – DCR Apr 11 '21 at 00:16
  • What shell are you using? – 0stone0 Apr 11 '21 at 00:19
  • 1
    I believe ssh (is that a shell?) whoops, when I do echo $SHELL i get /bin/bash – DCR Apr 11 '21 at 00:29
  • Then you should add the [tag:bash]-tag to the question! – 0stone0 Apr 11 '21 at 00:57

2 Answers2

1

We can use the ${target/search/replace} parameter expansion to replace the first occurrence of a pattern within a given string.

Combine that with find -exec to rename the folders.

find . -type d -iname 'section*' -maxdepth 1 -exec bash -c 'mv "$0" "${0/section/video}"' {} \;
  • find .: Find in the current directory
  • -type d: Only search for folders (use -type f for files)
  • -iname 'section*': Search pattern
  • -maxdepth 1: Restrict find from going deep
  • -exec bash -c 'mv "$0" "${0/section/video}"' {} \;
    • -exec run the following command for each path found
    • pass {} (path found by find) as argument so we can use $0
    • mv"$0" "${0/section/video}"
      • Move the file/folder from the current position
      • To the new position where we use the above mentioned bash syntax to replace section with video

Example:

  • Navigate to the desired folder
    This answer assumes the search and replace pattern only appear once in the path
$ cd /tmp/test
$
$
$ rm -rf *
$ mkdir section\ 1 section\ 2 section\ 35
$
$
$ find .
.
./section 1
./section 35
./section 2
$
$ find . -type d -iname 'section*' `-maxdepth 1` -exec bash -c 'mv "$0" "${0/section/video}"' {} \;
$
$ find .
.
./video 2
./video 1
./video 35
$
$

Use with caution

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    this is great. Can you provide a reference to the documentation? – DCR Apr 11 '21 at 01:55
  • Glad it helped. Added some more links. If I missed any, let me know. – 0stone0 Apr 11 '21 at 02:20
  • Sorry it took so long. Changed my answer to give an example renaming folders. Had some issues with quoting and an error. Solved by using `-maxdepth 1` to prevent `find` from searching for folders that we've already renamed. Please let me know if I missed something. – 0stone0 Apr 12 '21 at 15:01
  • 1
    looks good. Thanks a lot for coming back. – DCR Apr 12 '21 at 15:16
0

There is a very nice shell script which can perform batch renaming: vimv.

Warning: before use, please read carefully the README, especially the "Gotchas" section at the end.

It takes a list of files as parameters (or defaults to the files of the current directory) and opens your favourite editor with a temporary file where each line is one of the file paths you gave. Then you just have to modify any path in order to move or rename it.

The advantage is that you can perform very complex renamings, depending on the editor you are using.

In your case, you could do the following:

$ ls
'section 1'  'section 10'  'section 11'  'section 12'  'section 2'  'section 3'  'section 4'  'section 5'  'section 6'  'section 7'  'section 8'  'section 9'
$ vimv section*
[Inside Vim editor...] :%s/section/video
[Inside Vim editor...] :wq
12 files renamed.
$ ls
'video 1'  'video 10'  'video 11'  'video 12'  'video 2'  'video 3'  'video 4'  'video 5'  'video 6'  'video 7'  'video 8'  'video 9'
yolenoyer
  • 8,797
  • 2
  • 27
  • 61