Ridalgo
Ridalgo

Reputation: 701

Remove first n character from bunch of file names with cut

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.

Upvotes: 48

Views: 98706

Answers (5)

Murphy
Murphy

Reputation: 41

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

Upvotes: 4

Saumil
Saumil

Reputation: 2517

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

Upvotes: 2

Aman
Aman

Reputation: 1197

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

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

Upvotes: 82

armtatoo
armtatoo

Reputation: 441

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})//' *

Upvotes: 34

legoscia
legoscia

Reputation: 41548

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.

Upvotes: 13

Related Questions