Learn GNU/Linux Commands (3): Create/Copy Directories/Files - mkdir, touch, >, >>, cp rsync

Commands we mentioned can be achieved with the mouse in a desktop environment, but they are worth remembering. With these commands, we can easily write scripts to do tasks automatically.


Make Directory(ies)

mkdir [DIRECTORY]...

Make (create) the DIRECTORY(IES) if they do not already exist.

We can make directories not only in the current directory. For example:

[texpion@com ~]$ mkdir /tmp/mydir1
[texpion@com ~]$
This command makes a directory under "tmp". Note that all the ancestor directories must exist and the parent directory must be accessible to the user.

We can make multiple directories with one command. For example:

[texpion@com ~]$ mkdir mydir1 /tmp/mydir1
[texpion@com ~]$
This command makes "mydir1" under the current directory and "mydir1" under "tmp".


Make File(s)

touch [FILE]...

Update the access and modification times of each FILE to the current time.

This command can be used to create empty files.

The usage is similar to "mkdir". We can make multiple files with one command. And we can make files not only in the current directory.


> [FILE]

Normally, the shell give its output data to the standard output, which is the screen, as we normally see. ">" redirects the output to FILE.

Since there is no command, if FILE doesn't exist, an empty file named FILE will be created.

Note that if FILE exists, all its content will be replaced with the output, in this case, nothing.


>> [FILE]

">>" also redirects the output to FILE, but if FILE exists, it will append the output to FILE. In our case, since there is nothing on the left, if FILE exists, nothing will be changed.

This is the best way to create one empty file. If the file exists, it won't flush the file like ">", nor will it change the timestamps like "touch" does. And all GNU/Linux distributions can do this. This method is also short.

We can also create a file with text editors. We introduce text editors in Part 5.


Copy

Copy Files

cp SOURCE DEST

Copy SOURCE to DEST

In this command, SOURCE and DEST are both normal files instead of directories. If DEST doesn't exist, a copy of SOURCE will be created. Otherwise, the content of DEST will be replaced with the content of SOURCE.
cp SOURCE... DIRECTORY

Copy multiple SOURCE(s) to DIRECTORY.

In this command, the last pathname must be of a directory. All the files before DIRECTORY will be copied to DIRECTORY.


cp -t DIRECTORY SOURCE...

In this command, the pathname right behind -t must be of a directory. All the files after DIRECTORY will be copied to DIRECTORY.

The default behavior of "cp" is to replace existing files. We can change that with arguments below:

  • -n, --no-clobber: do not overwrite an existing file (overrides a previous -i option)
  • -i, --interactive: prompt before overwrite (overrides a previous -n option)
  • -u, --update: copy only when the SOURCE file is newer than the destination file or when the destination file is missing


Copy directory(ies)

cp -r SOURCE... DEST

-R, -r, --recursive: copy directories recursively

If DEST is a directory, SOURCE as well as all files and directories under SOURCE (if it is a directory) will be copied under DEST.

If DEST doesn't exist, all files and directories under SOURCE will be copied under a newly made directory called DEST.


Make archives

cp -a

-a, --archive: copy directories recursively; preserve mode, ownership, timestamps and all other possible additional attributes; never follow symbolic links.

Option -a includes the effect of -r. it preserves all attributes of files and directories. It copies symbolic links instead of the files they refer to. We will talk about symbolic links later.

The shell equivalent of copying in a desktop environment such as Windows is the following command:

cp -ai

Another method to copy

rsync --info=progress2 -auv

"rsync" is a file transfer program capable of efficient remote update via a fast differencing algorithm.

"rsync" is a more powerful program for copying. it performs better than "cp", especially when copying many files.

The usage for copying files is similar to "cp"

  • --info=progress2: display total transfer progress such as copied size, completion in %, speed, remaining time.
  • "-auv" have the similar effect as the options of "cp" with the same name.
  • "-v" displays what is being done.
  • --append: with this option, we can continue copying files that are stopped half way. This also works with files copied with "cp" previously.
  • --append-verify: like --append, compare checksums just to be sure.

Note: rsync may not be installed in your distribution.



Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

OBS Studio settings for best quality (loseless) recording

Install VMAF on Fedora, CentOS/Red Hat (RHEL)