Which command should you use to rename or move a file or directory )? In Linux?

You rename a directory by moving it to a different name. Use the mv command to rename directories.

$ pwd
/home/user2/veggies
$ ls
broccoli
$ mv broccoli carrots
$ ls
carrots

You can also use mv to move a directory to a location within another directory.

$ pwd
/home/user2/veggies
$ ls
carrots
$ mv carrots ../veggies2
$ ls ../veggies2
carrots

In this example, the directory carrots is moved from veggies to veggies2 with the mv command.

Get to grips with the file renaming powerhouse of the Linux world and give

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0—and yourself—a rest.
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
1 is flexible, fast, and sometimes even easier.  Here’s a tutorial to this powerhouse of a command.

What’s Wrong With mv?

There’s nothing wrong with

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 . The command does a fine a job, and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it’s always available. But sometimes you just need a bulldozer, not a shovel.

The

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 is not a dedicated file renaming tool.

Renaming a Single File With mv

To use

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 to rename a file type
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.

You can use 

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 to check the file has been renamed.

mv oldfile.txt newfile.txt
ls *.txt

Which command should you use to rename or move a file or directory )? In Linux?

Renaming Multiple Files with mv

Things get trickier when you want to rename multiple files.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 stands in stark contrast to the ease of using
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 to rename a single file.

Things escalate quickly.

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

How do we wrangle

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 into doing that for us? Let’s take a look at the files.

ls *.prog -l

Which command should you use to rename or move a file or directory )? In Linux?

Here’s one way to do it that doesn’t resort to writing an actual Bash script file.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done

Which command should you use to rename or move a file or directory )? In Linux?

DId that work? Let’s check the files and see.

ls *.pr*

Which command should you use to rename or move a file or directory )? In Linux?

So, yes, it worked. They’re all “.prg” files now, and there are no “.prog” files in the directory.

What Just Happened?

What did that long command actually do? Let’s break it down.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done

The first part starts a loop that is going to process each “.prog” file in the directory, in turn.

The next part says what the processing will do. It is using 

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
0 to move each file to a new file. The new file is going to be named with the original file’s name excluding the  “.prog” part. A new extension of “.prg” will be used instead.

The last part ends the loop after each file has been processed.

There Has to be a Simpler Way

Most definitely. It is the

ls *.pr*
3 command.

ls *.pr*
3 is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way. You’ll just have to substitute the appropriate command name according to the Linux flavor you’re using.

in Ubuntu and Debian-derived distributions you install

ls *.pr*
3 like this:

sudo apt-get install rename

Which command should you use to rename or move a file or directory )? In Linux?

In Fedora and RedHat-derived distributions you install

ls *.pr*
6 like this. Note the initial “p,” which stands for Perl.

sudo dnf install prename

Which command should you use to rename or move a file or directory )? In Linux?

To install it in Manjaro Linux use the following command. Note that the renaming command is called

ls *.pr*
7.

sudo pacman -Syu perl-rename

Which command should you use to rename or move a file or directory )? In Linux?

Let’s Do That Again

And this time we’ll use

ls *.pr*
3. We’ll roll back the clock so that we have a set of “.prog” files.

ls *.prog

Which command should you use to rename or move a file or directory )? In Linux?

Now let’s use the following command to rename them. We’ll then check with

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 whether it worked. Remember to substitute
ls *.pr*
3 with the appropriate command name for your Linux if you’re not using Ubuntu or a Debian-derived Linux.

ls *.txt
0
ls *.pr*

Which command should you use to rename or move a file or directory )? In Linux?

That worked, they’re now all “.prg” files, and there are no “.prog” files left in the directory.

What Happened This TIme?

Let’s explain that bit of magic, in three parts.

The first part is the command name,

ls *.pr*
3 (or
ls *.pr*
6 or
ls *.pr*
7 , for the other distributions).

The last part is

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
4, which tells
ls *.pr*
3 to operate on all “.prog” files.

The middle part defines the work we want to be done on each filename. The

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
6 means substitute. The first term (
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7) is what
ls *.pr*
3 will search for in each filename and the second term (
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
9)  is what it will be substituted with.

The middle part of the command, or central expression, is a Perl ‘regular expression‘ and it is what gives the

ls *.pr*
3 command its flexibility.

Changing Other Parts of a Filename

We’ve changed filename extensions so far, let’s amend other parts of the filenames.

In the directory are a lot of C source code files. All of the filenames are prefixed with “slang_”. We can check this with

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7.

ls *.txt
2

Which command should you use to rename or move a file or directory )? In Linux?

We are going to replace all occurrences of “slang_” with “sl_”. The format of the command is already familiar to us. We’re just changing the search term, the replacement term, and the file type.

ls *.txt
3

Which command should you use to rename or move a file or directory )? In Linux?

This time we are looking for “.c” files, and searching for “slang_”. Whenever “slang_” is found in a filename it is substituted with “sl_”.

We can check the result of that command by repeating the

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 command from above with the same parameters:

ls *.txt
2

Which command should you use to rename or move a file or directory )? In Linux?

Deleting Part of a Filename

We can remove a part of a filename by substituting the search term with nothing.

ls *.txt
5
ls *.txt
6
ls *.txt
5

Which command should you use to rename or move a file or directory )? In Linux?

We can see from the

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 command that our “.c” files are all prepended with “sl_”. Let’s get rid of that altogether.

The

ls *.pr*
3 command follows the same format as before. We’re going to be looking for “.c” files. The search term is “sl_”, but there is no substitution term. Two backslashes without anything between them means nothing, an empty string.

ls *.pr*
3 will process each “.c” file in turn. It will search for “sl_” in the filename. If it is found, it will be replaced by nothing. In other words, the search term is deleted.

The second use of the

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 command confirms that the “sl_” prefix has been removed from every “.c” file.

Limit Changes to Specific Parts of Filenames

Let’s use

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 to look at files that have the string “param” in their filename. Then we’ll use
ls *.pr*
3 to replace that string with the string “parameter”. We’ll use
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 once more to see the effect the
ls *.pr*
3 command has had on those files.

ls *.txt
8
ls *.txt
9
ls *.txt
8

Which command should you use to rename or move a file or directory )? In Linux?

Four files are found that have “param” in their filename. param.c, param_one.c, and param_two.c all have “param” at the start of their name. third_param.c has “param” at the end of its name, just before the extension.

The

ls *.pr*
3 command is going to search for “param” everywhere in the filename, and replace it with “parameter” in all cases.

The second use of the 

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 command shows us that that is exactly what has happened. Whether “param” was at the start or at the end of the filename, it has been replaced by “parameter.”

We can use Perl’s metacharacters to refine the behavior of the middle expression. Metacharacters are symbols that represent positions or sequences of characters. For example,

sudo dnf install prename
3 means “start of a string,”
sudo dnf install prename
4 means “end of a string,” and
sudo dnf install prename
5 means any single character (apart from a newline character).

We’re going to use the start of string metacharacter (

sudo dnf install prename
3 ) to restrict our search to the start of the filenames.

ls *.prog -l
1
ls *.prog -l
2
ls *.prog -l
1
ls *.prog -l
4

Which command should you use to rename or move a file or directory )? In Linux?

The files we renamed earlier are listed, and we can see the string “parameter” is at the start of three filenames and it is at the end of one of the filenames.

Our

ls *.pr*
3 command uses the start of line 
sudo dnf install prename
8)  metacharacter before the search term “parameter.” This tells
ls *.pr*
3 to only consider the search term to have been found if it is at the start of the filename. The search string “parameter” will be ignored if it is anywhere else in the filename.

Checking with

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7, we can see that the filename that had “parameter” at the end of the filename has not been modified, but the three filenames that had “parameter” at the start of their names have had the search string replaced by the substitute term “value.”

The power of

ls *.pr*
3 lies in the power of Perl. All of the power of Perl is at your disposal.

Searching With Groupings

ls *.pr*
3 has yet more tricks up its sleeve. Let’s consider the case where you might have files with similar strings in their names. They’re not exactly the same strings, so a simple search and substitution won’t work here.

In this example we use

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 to check which files we have that start with “str”. There are two of them, string.c and strangle.c. We can rename both strings at once using a technique called grouping.

The central expression of this

ls *.pr*
3 command will search for strings within filenames that have the character sequence “stri” or “stra” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “strang”. The substitution term is “bang”.

ls *.prog -l
5
ls *.prog -l
6
ls *.prog -l
7

Which command should you use to rename or move a file or directory )? In Linux?

Using 

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 a second time confirms that string.c has become bang.c and strangle.c is now bangle.c.

Using Translations With rename

The

ls *.pr*
3 command can perform actions on filenames called translations. A simple example of a translation would be to force a set of filenames into uppercase.

In the

ls *.pr*
3 command below notice that we’re not using an
sudo pacman -Syu perl-rename
8 to start the central expression, we’re using
sudo pacman -Syu perl-rename
9. This tells 
ls *.pr*
3 we’re not performing a substitution; we’re performing a translation.

The

ls *.prog
1 term is a Perl expression that means all lowercase characters in the sequence from a to z. Similarly, the
ls *.prog
2 term represents all uppercase letters in the sequence from A to Z.

The central expression in this command could be paraphrased as “if any of the lowercase letters from a to z are found in the filename, replace them with the corresponding characters from the sequence of uppercase characters from A to Z.”

To force the filenames of all “.prg” files to uppercase, use this command:

rename ‘y/a-z/A-Z/’ *.prg

ls *.prog -l
8

Which command should you use to rename or move a file or directory )? In Linux?

The

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"; done
7 command shows us that all of the “.prg” filenames are now in uppercase. In fact, to be strictly accurate, they’re not “.prg” files anymore. They’re “.PRG” files. Linux is case sensitive.

We can reverse that last command by reversing the position of the

ls *.prog
1 and
ls *.prog
2 terms in the central expression.

rename ‘y/A-Z/a-z/’ *.PRG

ls *.prog -l
9

Which command should you use to rename or move a file or directory )? In Linux?

You (Wo|Do)n’t Learn Perl in Five Minutes

Getting to grips with Perl is time well spent. But to start using the time-saving capabilities of the

ls *.pr*
3 command, you don’t need to have much Perl knowledge at all to reap large benefits in power, simplicity and time.

Which command is used to rename the directory name in Linux?

You rename a directory by moving it to a different name. Use the mv command to rename directories. You can also use mv to move a directory to a location within another directory.

Which Linux command is used to move a file from a directory?

To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp. Common options available with mv include: -i -- interactive.