Page Body

How to Create, Copy, Move, and Remove Objects Using the GNU/Linux Command Line

Knowing how to manipulate file system objects via the command line interface (CLI) is a fundamental part of the GNU/Linux experience.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.

Creating Objects

The objects you need to manipulate are files and directories.

Creating Text Files

You can create a text file with the touch command:

touch ex_file.txt...

Alternatively, you can use redirection:

> ex_file.txt

Normally, a terminal's standard output (/dev/stdout) is sent to the terminal display. Above, the > sign (short for 1>, where 1 is the file descriptor associated with the standard output) redirects the terminal's standard output to the specified file, ex_file.txt.

If ex_file.txt does not exist when the command is run, it is created. If ex_file.txt does exist when the command is run, it will most likely be clobbered (i.e., overwritten), depending on how your shell is configured.

Creating Directories

The mkdir command creates a directory.

mkdir ex_directory...

If you need to simultaneously create a directory and its parent directories, you can use mkdir's -p option (short for --parents).

mkdir -p /ex_directory_1/ex_directory_2/ex_directory_3

Above, ex_directory_1, ex_directory_2, and ex_directory_3 are simultaneously created.

Copying Objects

Files and directories can be copied with the cp command.

Copying Files

You can copy a file with cp as follows:

cp ex_file... ex_destination_directory

For example, the command below copies the file foo.txt in the current directory to the /tmp directory:

cp 'foo.txt' '/tmp'

Alternatively, you can use cp to create a copy of a file in the current directory with a new name.

cp ex_file new_ex_file

Below, a copy of foo.txt in the current directory is created in the same directory, but with the name bar.txt:

cp 'foo.txt' 'bar.txt'

cp works well with shell search patterns. For example, you can copy any files in the current directory that start with foo and end with .txt to the /tmp directory like this:

cp foo*.txt '/tmp'

Above, the * character will match any character (except for /) from zero to many times. If you are not sure how the shell will expand foo*.txt, you can always pass it to the echo command to verify how the shell will interpret your search pattern before you use it with the cp command.

$ echo foo*.txt
foo_1.txt foo_2.txt foo_3.txt foo_4.txt foo_5.txt

cp does not ask whether it should overwrite an existing file with the same name as the file to be copied, i.e., it just does it. When cp does this, the destination file is opened for writing and truncated to a length of zero before the source data is written to it.

If you do not have write access to the existing file, the copy operation will fail. However, you can override the failure with cp's -f option (short for --force). This will cause cp to remove the existing file you do not have write access to before proceeding with its copy operation.

By default, cp will copy the target of symbolic links, not the symbolic links themselves, although you can change this behavior with the -d or -P options.

Here are some of cp's most useful options:

Back up a copy of existing destinations by appending a tilde (~) to their names.
Copy symbolic links, not their targets; equivalent to -P.
Force copy and overwrite existing destination file without prompting (i.e., when an existing destination file cannot be written to, the existing file is removed, and then the copy operation proceeds).
Interactively ask (once per file) whether existing destination files should be overwritten.
Create hard links of files, instead of copying them (referred to as creating a link farm).
Try to preserve all attributes of the source file for the copy.
Copy symbolic links, not their targets. Equivalent to -d.
Create symbolic links of files, instead of copying them (referred to as creating a symbolic link farm).
Short for --update. Only copy if the source file is newer than the destination file (or the destination file does not exist).
Verbose output.

Copying Directories

To copy a directory, you can use cp's -R option.

cp -R ex_directory... ex_destination_directory

The following example copies the foobar directory in the current directory to ~/Documents:

cp -R 'foobar' ~/Documents

Moving Objects

To move a file or directory, you can use the mv command.

mv ex_object... ex_destination_directory

The command below moves the foobar directory in the current directory to ~/Downloads:

mv 'foobar' ~/Downloads

The mv command is similar to the cp command and has many of the same options. Like cp, mv also does not ask for confirmation if a destination object exists. It just overwrites it and has a -f option to force the operation when you do not have write permissions for the destination object.

A mv operation is strictly a file system operation on directory contents. An exception is if an object is moved to a different file system. In this case, in order to physically move the object, it is first copied to the new file system, and then removed from the old one.

In addition, the mv command can be used to rename a file or directory.

mv ex_object new_ex_object

For example, the following command renames the foobar directory in the current directory to xyzzy:

mv 'foobar' 'xyzzy'

Removing Objects

The rm command removes files and directories.

Removing Files

You can remove a file with rm as follows:

rm ex_file...

By default, rm removes files without any confirmation.

However, rm will prompt you to proceed with an entire removal operation if:

  • The -I option (short for --interactive=once) is used
  • The -R option (equivalent to -r or --recursive) is used
  • There are more than three files involved in the removal operation

If the response given is not affirmative, the entire removal operation is aborted.

Also, rm will prompt you to proceed with removing individual files if:

  • The -i option (short for --interactive=always) is used
  • You do not have write access to a file in the removal operation
  • The standard input is a terminal

If the response given is not affirmative, the file in question is skipped.

rm's -f option can be used to ignore non-existent files and prevent removal prompts.

When rm is given a symbolic link as an argument, it removes the symbolic link itself, not the target that it points to.

Removing Directories

If you need to remove an empty directory, you can do so with the rmdir command.

rmdir ex_empty_directory...

You can simultaneously remove an empty directory and its empty parent directories with rmdir's -p option (short for --parents).

rmdir -p /ex_directory_1/ex_directory_2/ex_directory_3

If a directory is not empty, you can use the previously mentioned rm command with the addition of its -f and -R options to remove it.

rm -fR ex_non_empty_directory...


You can find out more about the touch, mkdir, cp, mv, rm, and rmdir commands by examining their man pages, which are also available online.


Enjoyed this post?

Subscribe to the feed for the latest updates.