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 example_file.txt

Alternatively, you can use redirection:

> example_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, example_file.txt.

If example_file.txt does not exist when the command is run, it is created. If example_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 can be used to create a directory.

mkdir example_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 /example_directory_1/example_directory_2/example_directory_3

Above, example_directory_1, example_directory_2, and example_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 example_file example_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 example_file new_example_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:

-b
Back up a copy of existing destinations by appending a tilde (~) to their names
-d
Copy symbolic links, not their targets; equivalent to -P
-f
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)
-i
Interactively ask (once per file) whether existing destination files should be overwritten
-l
Create hard links of files, instead of copying them (referred to as creating a link farm)
-p
Try to preserve all attributes of the source file for the copy
-P
Copy symbolic links, not their targets; equivalent to -d
-s
Create symbolic links of files, instead of copying them (referred to as creating a symbolic link farm)
-u
Short for --update; only copy if the source file is newer than the destination file (or the destination file does not exist)
-v
Verbose output

Copying Directories

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

cp -R example_directory example_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 example_object example_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 example_object new_example_object

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

mv 'foobar' 'xyzzy'

Removing Objects

The rm command can be used to remove files and directories.

Removing Files

You can remove a file with rm as follows:

rm example_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 example_empty_directory

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

rmdir -p /example_directory_1/example_directory_2/example_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 example_non_empty_directory

Documentation

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.

Avatar

Enjoyed this post?

Subscribe to the feed for the latest updates.