left-icon

Linux Succinctly®
by Jason Cannon

Previous
Chapter

of
A
A
A

CHAPTER 8

Deleting, Moving, and Renaming Files and Directories

Deleting, Moving, and Renaming Files and Directories


Files and directories can be deleted with the rm command.

rm file   Remove file.

rm -r directory   To remove a directory with rm, the -r argument is required. The -r argument tells rm to remove files and directories recursively.

rm -f file   Use the -f option to force removal without prompting for confirmation.

Search patterns in the form of wildcards can be used with commands like rm and ls. The most commonly used wildcards are the asterisk and the question mark. The asterisk matches anything, while the question mark matches a single character. Remember that files and directories that begin with a period are considered hidden and will not be matched by the asterisk. To include the hidden file in your search pattern, start your search with a period.

$ ls

Desktop  Documents  Downloads  goals.txt  Music  Pictures  to-do.txt

$ ls t*

to-do.txt

$ rm t*

$ ls t*

ls: cannot access t*: No such file or directory

$ ls g*txt

goals.txt

$ ls g????????

goals.txt

$ ls g?

ls: cannot access g?: No such file or directory

$ ls -d .*

.  ..  .bash_history  .bash_logout  .bashrc  .hidden  .profile

$ rm .hidden

$

The cp command is used to copy files and directories. To create a copy, run cp source_file destination_file. You can also copy one or more files to a directory by ending the cp command with a destination directory.

cp source_file destination_file   Copy the source_file to the destination_file.

cp source_file1 [source_fileN ...] destination_directory   Copy the source_files to the destination_directory.

cp -i source_file destination_file   Use the -i option of cp to run in interactive mode. If the destination_file exists, cp will give you the opportunity to abort the operation or continue by overwriting the destination_file.

cp -r source_directory destination_directory   The -r option of cp causes the source_directory to be recursively copied to the destination_directory. If the destination_directory exists, the source directory is copied into the destination_directory. Otherwise the destination_directory will be created with the contents of the source_directory.

$ ls

1file

$ cp 1file 2file

$ ls

1file  2file

$ mkdir 1dir

$ cp 1file 2file 1dir

$ ls 1dir/

1file  2file

$ cp -i 2file  1file

cp: overwrite `1file'? n

$ cp -r 1dir 2dir

$ ls 2dir/

1file  2file

$ cp 1dir 3dir

cp: omitting directory `1dir'

$ mkdir 3dir

$ cp -r 1dir 2dir 3dir

$ ls 3dir

1dir  2dir

$ tree 3dir

3dir

|-- 1dir

|   |-- 1file

|   |-- 2file

|-- 2dir

     |-- 1file

     |-- 2file

2 directories, 4 files

$

To move files and directories from one location to another, use the mv command. Additionally, the mv command is used to rename files and directories.

mv source destination   Moves source to destination. If destination is a directory, source will be moved into destination. If destination is not a directory, then source will be renamed destination.

mv -i source destination   Use the -i option of mv to run in interactive mode. If the destination exists, mv will give you the opportunity to abort the operation or continue by overwriting the destination.

In the following example, 1dir is renamed to 1dir-renamed using the mv command. Next, 1file is renamed to file1 and then moved into the 1dir-renamed directory. If you do not specify the -i option to mv, it will overwrite an existing file without prompting you. This is demonstrated by moving 1file to 2file. Finally, the -i option is demonstrated with 2file and file1.

$ ls -F

1dir/  1file  2dir/  2file  3dir/

$ mv 1dir 1dir-renamed

$ ls -F

1dir-renamed/  1file  2dir/  2file  3dir/

$ mv 1file file1

$ ls -F

1dir-renamed/  2dir/  2file  3dir/  file1

$ mv file1 1dir-renamed/

$ ls -F

1dir-renamed/  2dir/  2file  3dir/

$ ls -F 1dir-renamed/

1file  2file  file1

$ cat 1dir-renamed/1file

The contents of 1file.

$ cat 1dir-renamed/2file

The contents of 2file.

$ mv 1dir-renamed/1file 1dir-renamed/2file

$ cat 1dir-renamed/2file

The contents of 1file.

$ ls -F 1dir-renamed/

2file  file1

$ mv -i 1dir-renamed/2file 1dir-renamed/file1

mv: overwrite `1dir-renamed/file1'? n

$

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.