Rename all files in folder linux

Rename all files in a folder with a prefix in a single command

If your filenames contain no whitepace and you don’t have any subdirectories, you can use a simple for loop:

$ for FILENAME in *; do mv $FILENAME Unix_$FILENAME; done 

Otherwise use the convenient rename command (which is a perl script) — although it might not be available out of the box on every Unix (e.g. OS X doesn’t come with rename ).

A short overview at debian-administration.org:

If your filenames contain whitespace it’s easier to use find , on Linux the following should work:

$ find . -type f -name '*' -printf "echo mv '%h/%f' '%h/Unix_%f\n'" | sh 

On BSD systems, there is no -printf option, unfortunately. But GNU findutils should be installable (on e.g. Mac OS X with brew install findutils ).

$ gfind . -type f -name '*' -printf "mv \"%h/%f\" \"%h/Unix_%f\"\n" | sh 

Also would recommend for f in *; do [[ -f $ ]] && mv . ; done to catch only files (no sub-directories, links, etc.).

If you quote variables as you should, then for FILENAME in *; do mv «$FILENAME» «Unix_$FILENAME»; done works correctly regardless of what characters are in the file names. It does move directories, sockets, symlinks and other file types too; I presume that doesn’t matter.

Try the rename command in the folder with the files:

The argument of rename (sed s command) indicates to replace the regex ^ with Unix_. The caret (^) is a special character that means start of the line.

this is awesome! i’d suggest to add folder/* , because * is a bit dangerous if command accidently will be repeated in another place

I think this is just what you’er looking for:

Yes, it is simple yet elegant and powerful, and also one-liner. You can get more detailed intro from me on the page:Rename Files and Directories (Add Prefix)

Beware of processing the output of ls — it can lead to problems if there are spaces or other oddball characters in the file names.

is it possible to replace certain chracter(s) while renaming. For example, if the file name is 2.0.2.CR1.zip, it should become 2.0.2.GA.zip

I recently faced this same situation and found an easier inbuilt solution. I am sharing it here so that it might help other people looking for solution.

With OS X Yosemite, Apple has integrated the batch renaming capabilities directly into Finder. Details information is available here. I have copied the steps below as well,

Rename multiple items

  1. Select the items, then Control-click one of them.
  2. In the shortcut menu, select Rename Items.
  3. In the pop-up menu below Rename Folder Items, choose to replace text in the names, add text to the names, or change the name format.
    • Replace text: Enter the text you want to remove in the Find field, then enter the text you want to add in the “Replace with” field.
    • Add text: Enter the text to you want to add in the field, then choose to add the text before or after the current name.
    • Format: Choose a name format for the files, then choose to put the index, counter, or date before or after the name. Enter a name in the Custom Format field, then enter the number you want to start with.
  4. Click Rename.
Читайте также:  Рестарт linux через консоль

If you have a common pattern in your files than you can use Replace text otherwise Add text would also do the job.

Источник

How to rename all files in a folder

And finally I would like to remove all dots (.) except the one at the end (.mp4) What commands should I use to create such script ?

Useful commands are grep, sed, and awk. What have you tried, and did it work, or fail? Have you searched for answers, with a search such as google.com/search?q=mass+rename+linux ? BTW, Linux 14.04 has reached end-of-life ubuntu.com/about/release-cycle and we are limited to provide support to current versions askubuntu.com/help/on-topic , such as 16.04 LTS, 18.04 LTS, 19.04, and alternate flavors ubuntu.com/download/flavours

«What commands should I use to create such script ?» script?! we got a «rename» command for that 😉 Have a look at man rename and man rename.ul .

I use Krusader file manager with krename . menu is file-multirename . you can prefilter using menu view-custom before that .

4 Answers 4

Firstly: Install rename by running the following command in the terminal:

Secondly: In the terminal, cd to the directory containing your files.

Finally: Rename the files to your desired format by running the following command in the terminal:

To see how the rename command will operate on your files but without really renaming them ( just print the output to the terminal ), you can add the option -n after it. Like so:

Explanation — as requested by Hamza:

Substitutes the ORIGINAL string with the NEW string.

To see how it works as simple as it gets:

  • Please run in the terminal rename -n ‘s/file.number1.2010.720p.otherinfo.mp4/NEW.mp4/’ *
  • Assuming you have a file named file.number1.2010.720p.otherinfo.mp4 in the current directory.
  • The output would be rename(file.number1.2010.720p.otherinfo.mp4, NEW.mp4)

Starts at the beginning of the string ^ and then matches one or more character/s ( any character ) (.+) before the dot \.

This group is put into a variable $1 and repeated four more times ( five in total ) each group is put into a variable ( $2 , $3 , $4 , $5 ) until .mp4 represented as \.mp4 is reached.

Makes sure the string ends with .mp4 by using the $ symbol which matches the end of the string.

This part is, however, a bit flexible and will give you undesired results if the file naming is inconsistent and you have files with more than five parts separated by dots in their names like file.number1.2010.720p.otherinfo.extrainfo.mp4

If this is the case, please substitute this part with the more strict regular expression below. This way it will only operate on files with five parts separated by dots in their names only:

Defines the new file name as what is under the variable for the first group $1 ( in this case file ) + what is under the variable for the second group $2 ( in this case number(x) ) + .mp4

Читайте также:  Установка vipnet safeboot linux

Operates on all the files in the current directory.

Источник

Rename files and directories recursively under ubuntu /bash

I want to rename all files and directories that contain the word «special» to «regular». It should maintain case sensitivity so «Special» won’t become «regular». How can i do this in bash recursively?

8 Answers 8

find /your/target/path/ -type f -exec rename 's/special/regular/' '<>' \; 

To rename directories only:

find /your/target/path/ -type d -execdir rename 's/special/regular/' '<>' \+ 

To rename both files and directories:

find /your/target/path/ -execdir rename 's/special/regular/' '<>' \+ 

@speakr, Great solution, thanks! Could you provide more details about what it is doing, especially this part » ‘<>‘ \ «? Quick goggling did not help, so I thought that this information may be useful for others as well.

@ThiloSchulz It does, but you have to use find … -execdir … ‘<>‘ + instead of … ‘<>‘ \; . Clarification added.

In case of many files, take advantage of regex in find, for the last example above you use: find /your/target/path/ -regex ‘.*special.*’ -execdir rename ‘s/special/regular/’ ‘<>‘ \+ , otherwise it will be quite slow!

Try doing this (require bash —version >= 4):

shopt -s globstar rename -n 's/special/regular/' ** 

Remove the -n switch when your tests are OK

warning

There are other tools with the same name which may or may not be able to do this, so be careful.

If you run the following command ( GNU )

$ file "$(readlink -f "$(type -p rename)")" 

and you have a result like

. /rename: Perl script, ASCII text executable 

then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo update-alternatives --set rename /path/to/rename 

(replace /path/to/rename to the path of your perl’s rename command.

If you don’t have this command, search your package manager to install it or do it manually

Last but not least, this tool was originally written by Larry Wall, the Perl’s dad.

I have about 1700 files to rename in directories in my home directory. That seems too many for this to work, that actually was the text in the error message, too many arguments.

People trying to rename a pattern occuring multiple times in the same name should add the ‘g’ flag at the end of the substitution command: ‘s/old/new/g’

If you don’t mind installing another tool, then you can use rnm:

rnm -rs '/special/regular/g' -dp -1 * 

It will go through all directories/sub-directories (because of -dp -1 ) and replace special with regular in their names.

@LokMac : I don’t think it’s available in homebrew (I only package binary for debian based OS), If you have automake, you can install it in any Unix system (Instructions are in the readme).

If using -execdir to transform both files and directories, you’ll also want to remove -type f from the example shown. To spell it out, use:

Читайте также:  Linux list all tcp connections

find /your/target/path/ -execdir rename ‘s/special/regular/’ ‘<>‘ \+

Also, consider adding g (global) flag to the regex if you want to replace all occurrences of special with regular in a given filename and not just the first occurrence. For example:

find /your/target/path/ -execdir rename ‘s/special/regular/g’ ‘<>‘ \+

will transform special-special.jpg to regular-regular.jpg . Without the global flag, you’ll end up with regular-special.jpg .

FYI: GNU Rename is not installed by default on Mac OSX. If you are using the Homebrew package manager, brew install rename will remedy this.

Here is another approach which is more portable and does not rely on the rename command (since it may require different parameters depending on the distros).

It renames files and directories recursively:

find . -depth -name "*special*" | \ while IFS= read -r ent; do mv $ent $regular$; done 

What it does

  • use find with -depth parameter to reorder the results by performing a depth-first traversal (i.e. all entries in a directory are displayed before the directory itself).
  • do pattern substitutions to only modifiy the last occurence of regular in the path.

That way the files are modified first and then each parent directory.

Giving the following tree:

├── aa-special-aa │ └── bb-special │ ├── special-cc │ ├── special-dd │ └── Special-ee └── special-00 

It generate those mv commands in that particular order:

mv ./aa-special-aa/bb-special/special-cc ./aa-special-aa/bb-special/regular-cc mv ./aa-special-aa/bb-special/special-dd ./aa-special-aa/bb-special/regular-dd mv ./aa-special-aa/bb-special ./aa-special-aa/bb-regular mv ./aa-special-aa ./aa-regular-aa mv ./special-00 ./regular-00 

To obtain the following tree:

├── aa-regular-aa │ └── bb-regular │ ├── regular-cc │ ├── regular-dd │ └── Special-ee └── regular-00 

As mentioned by Rui Seixas Monteiro it’s best to use the -iregex pattern option with the Find command. I’ve found the following works and includes the global flag in the regex as mentioned by U007D:

find /path/ -type f -iregex '.*special.*' -execdir rename 's/special/regular/g' '<>' \+;
find /path/ -type d -iregex '.*special.*' -execdir rename 's/special/regular/g' '<>' \+;
find /path/ -iregex '.*special.*' -execdir rename 's/special/regular/g' '<>' \+;

But it doesn’t do a depth first traversal so it’s open to problems with parent directories haven’t been renamed first.

For those just wanting to rename directories you can use this command:

find /your/target/path/ -type d -execdir rename 's/special/regular/' '<>' \; 

Note type is now d for directory, and using -execdir .

I haven’t been able to work out how to rename both files and directories in a single pass though.

Someone commented earlier that once it renamed the root folder then it couldn’t traverse the file tree any more. There is a -d switch available that does a depth traversal from the bottom-up, so the root would be renamed last I believe:

find -d /your/target/path/ -type d -execdir rename 's/special/regular/' '<>' \; 

From the manpage ( man find ):

 -d Cause find to perform a depth-first traversal, i.e., directories are visited in post-order and all entries in a directory will be acted on before the directory itself. By default, find visits directories in pre-order, i.e., before their contents. Note, the default is not a breadth-first traversal. 

Источник

Оцените статью
Adblock
detector