File Renaming Linux

How to rename a bunch of files

You can use the rename command as shown below:

rename NCI_ NCIB_ *

Check the screenshot for sample output.

Sample Image

Rename multiple files by replacing a particular pattern in the filenames using a shell script

An example to help you get off the ground.

for f in *.jpg; do mv "$f" "$(echo "$f" | sed s/IMG/VACATION/)"; done

In this example, I am assuming that all your image files contain the string IMG and you want to replace IMG with VACATION.

The shell automatically evaluates *.jpg to all the matching files.

The second argument of mv (the new name of the file) is the output of the sed command that replaces IMG with VACATION.

If your filenames include whitespace pay careful attention to the "$f" notation. You need the double-quotes to preserve the whitespace.

rename file using a loop

With your shown samples, could you please try following. This will only print the rename command on terminal(for safer side, check and make sure if commands are fine and looking ok to you first before actually renaming files), to perform actual rename remove echo from following.

for file in *_ideal.sdf
do
echo mv "$file" "${file/_ideal/}"
done

Rename files in linux using mv and regex

Providing that all of these *mp4 files are in the same directory you
can indeed use for loop like that:

for i in *mp4; do mv "$i" "$(echo "$i" | sed -E 's,^[0-9]{2}\.,,')"; done

You didn't specify what shell you use. In Bash, for example you could
use <<< instead of echo.

You could also use Perl implementation of rename program (there is
another program called rename which is a part of util-linux
package) like that:

rename 's,^[0-9]{2}\.,,' *mp4

rename command for replacing text in filename from a certain point (character), but only up to, and maintaining the file extension

This works in my specific case, but should work for any file extension.

rename -n 's/-.*(?=\.wav$)//' *

The command looks for all characters after and inclusive of the - symbol in the filename, then, using a positive lookahead** (?=\.wav$) to search for the characters (the file extension in this case) at the end of the filename (denoted by $, and replaces them with no characters (removing them).

** NOTE: A positive look ahead is a zero width assertion.
It will affect the match but it will not be included
in the replacement. (The '.wav' part will not be
erased)

In this example (?=\.wav$) is the positive lookahead. The dollar sign $, as in regex, denotes at the end of the line, so perfect for a file extension.

Renaming files in Linux to date minus one day

Assuming the format of FROM_DATE is YYYYMMDD the date command in your first script is useless. It does nothing except echoing what you pass it. And your second date command does exactly what you want. So all in all the following should make it:

y=$(date -d"$FROM_DATE -1 day" +%Y%m%d)
for f in $DATA_DIR/*.csv; do
mv "$f" "${f%.csv}__${y}_${y}000000.csv"
done


Related Topics



Leave a reply



Submit