43

I am using

ls | cut -c 5-

This does return a list of the file names in the format i want them, but doesn't actually perform the action. Please advise.

jww
  • 97,681
  • 90
  • 411
  • 885
Ridalgo
  • 651
  • 1
  • 6
  • 10
  • 25
    If you are on Mac or Linux, just open the terminal, CD to the folder with your files, and do `for f in *; do mv "$f" "${f:5}"; done`. This will 'cut' the first five chars from all the files in that folder. – Kebman Mar 23 '17 at 00:19
  • @Kebman that works perfectly. However if I try without CDing and point the directory through a variable, it doesn't work. The code: `search_dir=/Users/veller/Desktop/folder; for entry in "$search_dir"/*; do mv "$entry" "${entry:5}"; done` It gives me the following error: No such file or directory – Fazwelington Jan 15 '22 at 18:40
  • use `for f in *.*` in Kebman comment to avoid subfolders. – Ferroao Jul 28 '23 at 19:05

5 Answers5

79
rename -n 's/.{5}(.*)/$1/' *

The -n is for simulating; remove it to get the actual result.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
Aman
  • 1,157
  • 7
  • 13
  • 3
    For macOS, you can install `brew` https://brew.sh/ and then `brew install rename` to use this – spex Dec 06 '18 at 18:49
  • This doesn't work for me. I ran it on my folder, but nothing changed. – Ibraheem Ahmed Mar 18 '21 at 01:14
  • 3
    Ah, looks like I was using the `linux-util` version of `rename` instead of the perl script. For any arch users, you have to install the official `perl-rename` package. – Ibraheem Ahmed Mar 18 '21 at 01:22
32

you can use the following command when you are in the folder where you want to make the renaming:

rename -n -v  's/^(.{5})//' *

-n is for no action and -v to show what will be the changes. if you are satisfied with the results you can remove both of them

rename 's/^(.{5})//' *
armtatoo
  • 421
  • 4
  • 4
11

Something like this should work:

for x in *; do
    echo mv $x `echo $x | cut -c 5-`
done

Note that this could be destructive, so run it this way first, and then remove the leading echo once you're confident that it does what you want.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 1
    With the first `echo` removed and as written, wouldn't this only works without errors if the directory only contains files without spaces in the name and are at least 6 characters long? While double-quoting `$x` and using `$(..)` instead of legacy use of back-ticks would take of filename with spaces errors however that alone would not take care of all errors that could be thrown, e.g. if a filename is only 5 characters long or there are directories. – user3439894 Feb 03 '15 at 18:17
  • 4
    echo mv $x ${x:5} – Ivan Kovtun Nov 25 '19 at 06:05
  • Nice and straightforward. You can use this to verify the new filename before adding the mv command to the one-liner: `for fn in *; do newfn=$(echo $fn | cut -c 19-); echo will move $fn to $newfn; done` – JL_SO Jan 10 '20 at 10:36
3

Kebman's little code is nice for if you want to cut off the leading dot of hidden files and folders in the current dir, before 7zipping or zipping.

I put this in a bash script, but this is wat I mean:

for f in .*; do mv -v "$f" "${f:1}"; done # cut off the leading point of hidden files and dirs

7z a -pPASSWORD -mx=0 -mhe -t7z ${DESTINATION}.7z ${SOURCE} -x!7z_Compress_not_this_script_itself_*.sh # compress all files and dirs of current dir to one 7z-file, excluding the script itself.

zip and 7z can have trouble with hidden files at top level in the current dir. Hidden files in the subdirs are accepted.

mydir/myfile = ok
mydir/.myfile = ok
.mydir/myfile = nok
.mydir/.myfile = nok
Murphy
  • 31
  • 1
2

If you get an error message saying,

rename is not recognized as the name of a cmdlet

This might work for you,

get-childitem * | rename-item -newname { [string]($_.name).substring(5) }
Saumil
  • 2,521
  • 5
  • 32
  • 54