Rename all files in directory 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,

Читайте также:  Нет звука vlc linux

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.

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 files in a directory and all subdirectories

How do you replace the phrase «Full Tilt» with «FullTilt» in all files in a directory and subdirectories under that directory (and all sub-directories under those subdirectories and so on)?

Are you renaming files or editing the contents of the files? Your title suggests renaming; your question suggests editing (‘in all files in a directory’). If you said ‘in all file names in a directory’, your question would be consistent with your title.

4 Answers 4

Haven’t tested it, but you should be able to do it more or less like this:

First, create a script called ‘rename.sh’, make it executable and save it with the following content:

#! /bin/bash folder=`dirname $1` oldname=`basename $1` newname=`echo "$oldname" | sed 's/Full Tilt/FullTilt/g'` mv "$folder/$oldname" "$folder/$newname" 

Then, run the following command from inside your directory:

find -name "*Full Tilt*" -type file -exec "rename.sh" "<>" ";" 

It can probably be done with a one-liner on find (instead of using a script), but it won’t be readable and it will contain too many escapes to follow.

find . -name '*xx*' -print | sed -e 's/^\(.*\)xx\(.*\)$/\1xx\2 \1yy\2 /g' | xargs -n2 mv 

Where ‘xx‘ ist the search string for the file names. sed replaces the xx with yy and pipes that to xargs xargs takes now (-n2) two parameters (old and new name) and execute mv with both parms.

That is only the start point, try it in a safe environment 🙂

You have first rename the files ( -type=f ) after that you rename the path ( -type=d ). If you change this order the mv will fail while the directory is renamed while changing the next file name 🙂

Читайте также:  Матрица в консоли linux

If there are spaces in the file names, you have to use print0 instead of print. But then you also have to change the sed part. I think the rest is your homework 🙂

Feel free to ask if something will not work at all.

Источник

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

Читайте также:  Linux command open application

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

Operates on all the files in the current directory.

Источник

bash script to rename all files in a directory?

Fixed problem in previous version that did not properly handle path names.

@levislevis85: fixed the problem with the pathname but it does work with newlines, it’s just that the echo makes you think otherwise.

It’s good that you came up with a solution that does all of those things. but note that the question was asked specifically about files in 1 directory, on Linux, with nothing unusual about the filenames.

@Code Duck: Adding recursive capability was a trivial task and one should never assume anything about filenames, always treat them as if they include newlines or other nasties. The second you do not is when somebody adds one that breaks your script horribly and does god knows what.

For your specific case, you want to use mmv as follows:

pax> ll total 0 drwxr-xr-x+ 2 allachan None 0 Dec 24 09:47 . drwxrwxrwx+ 5 allachan None 0 Dec 24 09:39 .. -rw-r--r-- 1 allachan None 0 Dec 24 09:39 file1.txt -rw-r--r-- 1 allachan None 0 Dec 24 09:39 file2.avi pax> mmv '*.*' '#1_#1.#2' pax> ll total 0 drwxr-xr-x+ 2 allachan None 0 Dec 24 09:47 . drwxrwxrwx+ 5 allachan None 0 Dec 24 09:39 .. -rw-r--r-- 1 allachan None 0 Dec 24 09:39 file1_file1.txt -rw-r--r-- 1 allachan None 0 Dec 24 09:39 file2_file2.avi 

You need to be aware that the wildcard matching is not greedy. That means that the file a.b.txt will be turned into a_a.b.txt , not a.b_a.b.txt .

The mmv program was installed as part of my CygWin but I had to

on my Ubuntu box to get it down. If it’s not in you standard distribution, whatever package manager you’re using will hopefully have it available.

If, for some reason, you’re not permitted to install it, you’ll have to use one of the other bash for -loop-type solutions shown in the other answers. I prefer the terseness of mmv myself but you may not have the option.

Источник

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