Difference between revisions of "Command Line Examples: mv"

From PHYSpedia
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
The mv command will do different things depending on how many arguments you give it.
 
The mv command will do different things depending on how many arguments you give it.
  
In the following examples, assume that we are in the directory <code>/home/user/work/examples</code>; there are two files named <code>file1.txt</code> and <code>file2.txt</code>; there is a sub-directory named <code>finished</code>.
+
In the following examples, assume that we are in the directory <code>/home/user/work/examples</code>; there are two files named <code>file1.txt</code> and <code>file2.txt</code>; there is a sub-directory named <code>finished</code>; there is also a directory named <code>/home/user/work/tmp</code> on the system.
 
Note that these examples could not be run in order.
 
Note that these examples could not be run in order.
  
Line 7: Line 7:
 
To "rename" file, just move it to a file with a different name
 
To "rename" file, just move it to a file with a different name
 
  <code>
 
  <code>
  user@host:examples$ mv file1.txt newname.txt
+
  user@host:~/work/examples$ mv file1.txt newname.txt
 
  </code>
 
  </code>
  
 
move a file to a subdirectory
 
move a file to a subdirectory
 
  <code>
 
  <code>
  user@host:examples$ mv file1.txt finished/
+
  user@host:~/work/examples$ mv file1.txt finished/
 
  </code>
 
  </code>
  
 
move two files to a subdirectory
 
move two files to a subdirectory
 
  <code>
 
  <code>
  user@host:examples$ mv file1.txt file2.txt finished/
+
  user@host:~/work/examples$ mv file1.txt file2.txt finished/
 
  </code>
 
  </code>
  
 
move all files ending with .txt to a subdirectory
 
move all files ending with .txt to a subdirectory
 
  <code>
 
  <code>
  user@host:examples$ mv *.txt finished/
+
  user@host:~/work/examples$ mv *.txt finished/
 +
</code>
 +
 
 +
move a file to the directory above
 +
<code>
 +
user@host:~/work/examples$ mv file1.txt ../
 +
</code>
 +
 
 +
move multiple files to the directory above
 +
<code>
 +
user@host:~/work/examples$ mv file1.txt file2.txt ../
 +
</code>
 +
 
 +
move a file to some other directory using the absolute path
 +
<code>
 +
user@host:~/work/examples$ mv file1.txt /home/user/work/tmp
 +
</code>
 +
 
 +
move a file to some other directory using the relative path
 +
<code>
 +
user@host:~/work/examples$ mv file1.txt ../tmp
 +
</code>
 +
 
 +
move a file to a different directory AND rename it
 +
<code>
 +
user@host:~/work/examples$ mv file1.txt ../tmp/finished.txt
 +
</code>
 +
 
 +
move a directory to a different directory
 +
<code>
 +
user@host:~/work/examples$ mv finished/ ../tmp/
 +
</code>
 +
 
 +
move a directory to a different directory AND rename it
 +
<code>
 +
user@host:~/work/examples$ mv finished/ ../tmp/unfinished
 
  </code>
 
  </code>

Latest revision as of 09:54, 7 February 2013

The mv command will do different things depending on how many arguments you give it.

In the following examples, assume that we are in the directory /home/user/work/examples; there are two files named file1.txt and file2.txt; there is a sub-directory named finished; there is also a directory named /home/user/work/tmp on the system. Note that these examples could not be run in order.


To "rename" file, just move it to a file with a different name


user@host:~/work/examples$ mv file1.txt newname.txt

move a file to a subdirectory


user@host:~/work/examples$ mv file1.txt finished/

move two files to a subdirectory


user@host:~/work/examples$ mv file1.txt file2.txt finished/

move all files ending with .txt to a subdirectory


user@host:~/work/examples$ mv *.txt finished/

move a file to the directory above


user@host:~/work/examples$ mv file1.txt ../

move multiple files to the directory above


user@host:~/work/examples$ mv file1.txt file2.txt ../

move a file to some other directory using the absolute path


user@host:~/work/examples$ mv file1.txt /home/user/work/tmp

move a file to some other directory using the relative path


user@host:~/work/examples$ mv file1.txt ../tmp

move a file to a different directory AND rename it


user@host:~/work/examples$ mv file1.txt ../tmp/finished.txt

move a directory to a different directory


user@host:~/work/examples$ mv finished/ ../tmp/

move a directory to a different directory AND rename it


user@host:~/work/examples$ mv finished/ ../tmp/unfinished