Linux+: Linux Shell 08 – Shell Commands Part 5

J

Jarret W. Buse

Guest
Linux+: Linux Shell 08 – Shell Commands Part 5

So far in the Shell Commands portion of the Linux+ series, we have covered changing directories (cd), copying (cp), finding files (find) and listing (ls) files and folders. Now, we will look into moving (mv) folders and files.

The move command is somewhat simple with few parameters and is very similar to the copy (cp) command. Remember that the file is copied to the target location and then the source file is deleted.

The syntax for the move command is as follows:

mv [OPTIONS] [SOURCE] [DESTINATION]
mv [OPTIONS] [SOURCE] [DIRECTORY]
mv [OPTIONS] [SOURCE1] SOURCE2] [SOURCE-#] [DESTINATION]
mv [OPTIONS] [–target-directory=DIRECTORY] [SOURCE]


Any of these are valid depending on the requirement of the move.

NOTE: Be aware that the source name can include wildcards to allow for all files by using an asterisk (*).

The first option is to specify any options, the source file to move with its location, and then the new filename. This syntax form is a simple rename using a file in the current directory with a name of 'toff' and renaming it to 'touchoff':

mv toff touchoff

If you are not in the current directory of the file, say it is in the home folder, and you wish to perform the same renaming:

mv ~/toff ~/touchoff

The second syntax form is to move a file from one location to another.

For example, to move a file, 'toff', from the home folder (~) to the /usr/bin folder with the keeping the same name would be:

mv ~/toff /usr/bin/

Of course, if you were currently in the home folder, you could use the following command:

mv toff /usr/bin/

It is possible to copy all files from the home folder to the /media/sda2/data/ folder as such:

mv ~/* /media/sda2/data/

It is also possible to move whole directories to another folder. Assume there is a folder named 'files' in the home directory which needs to be moved to the 'media/sda3/work/' folder. If we are currently in the home folder, the following command would work:

mv files /media/sda3/work/

The third syntax allows for multiple folders to be specified to move to the destination directory. If we have three folders in the home directory, which is our current folder, named 'work1', 'work2' and 'work3' and we wish to move them all to the '/media/sda4/work' folder:

mv work1 work2 work3 /media/sda4/work/

The final syntax form is to use the option to specify the target directory (-target-directory).

For example, to copy the file 'toff' from the home folder to the /usr/bin folder would be:

mv -target-directory=/usr/bin/ ~/toff

It is important to know the various options for the move (mv) command. These options are as follows:

  • --backup=CONTROL – makes a backup of each existing file which is in the target location
    • none, off – never perform a backup
    • numbered – make numbered backups
    • existing, nil – perform a numbered backup if they already existing
    • simple, never – always perform a simple backup
    -b – same as '--backup', but does not require 'CONTROL' parameters. To specify a suffix, use the '-s' parameter and specify the suffix you wish to use
    -f (--force) – if a file needs to be overwritten do not prompt before overwriting (same as --reply=yes)
  • -i (--interactive) – prompts before overwriting any existing files (same as --reply=query)
  • --reply=ANSWER – specifies how user prompts should be handled, usually for answering how an existing file should be handled for overwriting
    • yes – all prompts should be answered 'yes'
    • no – all prompts should be answered 'no'
    • query – user should be prompted
  • --strip-trailing-slashes – removes the slash (/) from the end of the source filename
  • -s=SUFFIX (-suffix=SUFFIX) – specifies the suffix to add to a backup filename. The default suffix is the tilde (~) and is used with the '-b' parameter
  • -target-directory=LOCATION – specifies the target directory instead of using the last parameter as the target
  • -u (--update) – moves files from source to target only when the source file is newer than the existing target file. If the target file does not exist, the file is moved
  • -v (--verbose) – provides more details about the moving of the source files
  • --help – displays help for the 'mv' command
  • --version – shows the version number of the mv command
The 'numbered' option moves the file normally the first time if the file does not exist. The second time, the file is copied and numbered. For example, after backing up the file 'Tron.jpg' to my Home folder twice, I will have the following two files:

  1. Tron.jpg
  2. Tron.jpg.~1~
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 37,706




Top