Linux Commands Examples

A great documentation place for Linux commands

mv

move (rename) files


see also : rename

Synopsis

mv [OPTION]... [-T] SOURCE DEST
mv
[OPTION]... SOURCE... DIRECTORY
mv
[OPTION]... -t DIRECTORY SOURCE...


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

3
source
            
mv 2-* logs/
mv *.result logs/
mv *.kml logs/
mv *.log logs/
mv *~ logs/
mv *.xml logs/
1
source
            
mv gpu-* logs/
mv *.result logs/
mv *.kml logs/
mv *.log logs/
mv *~ logs/
mv *.xml logs/
1
source
            
mv 1 0001
mv 2 0002
mv 3 0003
mv 4 0004
mv 5 0005
mv 6 0006
mv 7 0007
mv 8 0008
mv 9 0009
mv 10 0010
mv 10 0010
mv 11 0011
mv 12 0012
mv 13 0013
mv 14 0014
mv 15 0015
mv 16 0016
mv 17 0017
mv 18 0018
mv 19 0019
mv 20 0020
1
source
            
mv CONFIG CONFIG.OLD
mv REVCON CONFIG
mv REVIVE REVOLD
1
source
            
mv *.txt ./bak
1
source

How to move multiple directories, but exclude files in current path with similar name?

If your directory structure is only 1 level deep mv dir*/ destination will accomplish this task.

0
source
            
mv sleep-* logs/
mv *.result logs/
mv *.kml logs/
mv *.log logs/
mv *.xml logs/
0
source
            
mv .bash_profile ~/
mv .bash_prompt ~/
mv .bashrc ~/
mv .exports ~/
mv .aliases ~/
mv .gitconfig ~/
mv .gitignore_global ~/
mv .gitignore_global ~/
0
source
            
exec mv mud ../
0
source

Linux: How to move all files from current directory to upper directory?

You can use the following:

mv * ../
0
source

What happens if mv is interrupted?

No. mv operates object by object, so objects that have already been processed will be removed from the source.

0
source

mv command confuses directory name with command option

This is a standard issue with filenames/directories starting with less conventional symbols. Your problem is that mv is treating --pycache-- as long option name (since it starts with --, there are also short options, they start with -). Please see manpage for getopt for details about long and short options.

The standard workaround in this situation is to use an empty double dash -- before all argument, which tells the command (mv in your case, but will work with others, cp for example) to stop treating what follows as options and treat it as arguments.

Thus, your command will become:

$ mv -- --pycache--/ __pycache__

and won't fail.

0
source

How to move folder in unix using mv, only when target is on the same drive

Based on How to check if two directories or files belong to same filesystem (http://unix.stackexchange.com/):

In a shell script on Linux it can be done with stat:

stat -c "%d" /path  # returns the decimal device number

So you can:

file1=$(stat -c "%d" /path/file1)
file2=$(stat -c "%d" /path/file2)

and then compare.

You can also write like this:

if [ $(stat -c "%d" /path/file1) -eq $(stat -c "%d" /path/file1) ]
then
    # mv sentence
fi

Other option. Also taken from the Stackexchange question:

if [[ $(df -P /path/file1 | tail -1) = $(df -P /path/file2 | tail -1) ]]
then
    # echo "same filesystem"
    # mv sentence
fi
0
source

How to mv a folder in Linux retaining its mtime?

POSIX mv doesn't provide any option to ask for atime/mtime preservation, but as the operation is local to a same volume, you can ask cp to use hard-links instead of copying data of the regular files using the -l option:

cp -p -r -l source/date target/
rm -rf source/data

Since only directories and file references will be actually copied, it should go much faster:

For more informations on hard-links, you can consult the corresponding Wikipedia page

As for why subdirectories mtime is being reset with your current solution, it's because you only get and restore the parent directory mtime : touch is not a recursive command.

0
source

How to move only files in Unix

you can use find

find * -maxdepth 1 -type f -exec mv {} ~ \;
0
source

How can I log the output of the mv command in Linux?

Try mv -v -f sourceDir destDir > out.file 2> err.file

0
source

Move a range of numbered files?

This will move the files as you described (except that the second range would be 022 to 042 for the second 21 files).

for ((i = 1; i <= 291; i++))
do
    ((d = (i - 1) / 21 + 1))
    printf -v file 'filename%03d' "$i"
    printf -v dir  'dirname%02d'  "$d"
    [[ -d "$d" ]] && mkdir "$d"
    mv "$f" "$d"
done
0
source

Move files and folders recursively in linux

Unless I am misunderstanding the question wouldn't this work?

mv /public-back/templates/* /public/templates

also, unless you have a huge list of files adding -i will ask before it overwrites anything, which add some safety when using wildcards like *.

0
source

Linux mv command moved data to an unknown location?

If the alphabetically last entry in this directory is a directory itself, they are there.

Suppose you have the files a, b and c and the directory d. In this case,

mv /var/www/vhosts/website/httpdocs/magento/*

expands to

mv /var/www/vhosts/website/httpdocs/magento/a /var/www/vhosts/website/httpdocs/magento/b /var/www/vhosts/website/httpdocs/magento/c /var/www/vhosts/website/httpdocs/magento/d

which is a command to move everything to d. Look there if there is anything.

If d wasn't a directory, mv should have complained about that.

0
source

Literate way of moving all files to a new subdirectory

bash:

shopt -s extglob
mkdir backup && mv !(backup) backup
0
source

XCopy on server?

In your control center you should have a part where you can configure access. Among these options, there is FTP and also SSH. Configure SSH access, and log in. If you come via Windows, you have to have a SSH client. Putty is pretty much standard.

In your shell session, you've logged in to your home directory. Your move command is almost right, but you should use a path relative to your home (leave the initial slash away) and use forward slashes / instead of backslashes \. If you want to automatically generate a directory name with a date, you have to look into the basics of shell programming.

Experiment with this: mkdir "`date`" (that are quotes and backticks) and mv -r path/to/fromfolder path/to/tofolder

EDIT: Commenters are right, it should be noted that the date command between the backticks gets executed. Anything between the backticks will be executed and if it produces output, it will land there. And (of course), you should familiarize with the commands you use, for that (depending on how the host is configured) try: man date, info date and/or apropos date (and so on)

0
source

Don't move a directory/file if src and dest are on different partitions

You are almost certainly looking at writing a script to do this.

For files you could hardlink them and if that was successful, unlink the original. That won't work for directories (because most filesystems prohibit hardlinking directories).

Minimally tested, trivial script that might do it:

#/bin/sh
if ln "$1" "$2" ;
then
   unlink "$1"
fi

The alternative is to check the filesystem associated with each path.

Here I might start with something along the lines of

#! /bin/sh
STAT=/usr/bin/stat
v1=$( $STAT -f "%d" "$1" )
v2=$( $STAT -f "%d" "$2" )
if [[ $v1 == $v2 ]]
then
    mv "$1" "$2"
fi

WARNING! That has a bug if you specify the topmost directory of a filesystem as a target. Fixing the bug is subtle, but it might go like this: check if the destination already exists. If not touch it. Then stat the destination. If the copy fails and you had to create the target to test it, remove the target.

description

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup
[=CONTROL]

make a backup of each existing destination file

-b

like --backup but does not accept an argument

-f, --force

do not prompt before overwriting

-i, --interactive

prompt before overwrite

-n, --no-clobber

do not overwrite an existing file

If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes

remove any trailing slashes from each SOURCE argument

-S, --suffix=SUFFIX

override the usual backup suffix

-t, --target-directory=DIRECTORY

move all SOURCE arguments into DIRECTORY

-T, --no-target-directory

treat DEST as a normal file

-u, --update

move only when the SOURCE file is newer than the destination file or when the destination file is missing

-v, --verbose

explain what is being done

--help

display this help and exit

--version

output version information and exit

The backup suffix is ’~’, unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values:
none, off

never make backups (even if --backup is given)

numbered, t

make numbered backups

existing, nil

numbered if numbered backups exist, simple otherwise

simple, never

always make simple backups

copyright

Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report mv bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report mv translation bugs to <http://translationproject.org/team/>


see also

rename

The full documentation for mv is maintained as a Texinfo manual. If the info and mv programs are properly installed at your site, the command

info coreutils 'mv invocation'

should give you access to the complete manual.


author

Written by Mike Parker, David MacKenzie, and Jim Meyering.

How can this site be more helpful to YOU ?


give  feedback