Copying, Moving, and Deleting Files and Directories

Creating files and directories is only part of the story. In the GUI, you’ll drag files and directories to move them to new locations, and you’ll use the mouse to copy files from one place to another. And sometimes you’ll want to get rid of a file or an entire directory’s worth of files. You can do all of these from the CLI more quickly than you can with the GUI.

In the previous section, you created a directory structure that looks like this:

 files
 ├── code
 │   ├── elm
 │   ├── go
 │   └── js
 ├── docs
 │   ├── diagrams
 │   └── markdown
 ├── movies
 ├── music
 └── photos

Let’s create a few files in there. First, create some empty files in each subdirectory of the code directory. Use brace expansion to create these files quickly:

 $ ​​touch​​ ​​files/code/{elm/main.elm,go/main.go,js/app.js}

Create the files chapter1.md, chapter2.md, and chapter3.md in the files/docs/markdown directory. You can use brace expansion here too:

 $ ​​touch​​ ​​files/docs/markdown/chapter{1,2,3}.md

In fact, since the numbers for the chapters are sequential, you can use this command instead:

 $ ​​touch​​ ​​files/docs/markdown/chapter{1..3}.md

That’s pretty handy if you need to create a bunch of numbered files; Bash will expand the range automatically.

Before moving on, verify that your structure looks like this:

 $ ​​tree​​ ​​files
 files
 ├── code
 │   ├── elm
 │   │   └── main.elm
 │   ├── go
 │   │   └── main.go
 │   └── js
 │   └── app.js
 ├── docs
 │   ├── diagrams
 │   └── markdown
 │   ├── chapter1.md
 │   ├── chapter2.md
 │   └── chapter3.md
 ├── movies
 ├── music
 └── photos

Now that the structure is in place, let’s copy some files around.

Copying Files

To copy a file, use the cp command, and specify the source and destination paths.

Try this out by creating a README.md file in the files/code/elm directory. You’ll then copy that file to the other code directories:

 $ ​​echo​​ ​​"# Project README"​​ ​​>​​ ​​files/code/elm/README.md

To copy this file to the files/code/go directory, use the cp command and specify the source path, files/code/elm/README.md, and then the destination path, files/code/go/README.md:

 $ ​​cp​​ ​​files/code/elm/README.md​​ ​​files/code/go/README.md

The file is copied to the new location.

You don’t actually need to specify the destination filename. Copy the same README.md file to the files/code/js directory, but this time, don’t specify the destination filename:

 $ ​​cp​​ ​​files/code/elm/README.md​​ ​​files/code/js/

When you don’t specify a filename, the cp command uses the source filename.

However, specifying the destination name lets you copy the file and rename the new version at the same time. In the files/documents directory, you have three Markdown files. Create a fourth one by copying chapter1.md to chapter4.md:

 $ ​​cp​​ ​​files/docs/markdown/chapter1.md​​ ​​files/docs/markdown/chapter4.md

The cp command always replaces the destination file if it exists. Run the command again:

 $ ​​cp​​ ​​files/docs/markdown/chapter1.md​​ ​​files/docs/markdown/chapter4.md

You get absolutely no warning. Be careful when copying files around; it’s very easy to specify the wrong destination and lose data. If you don’t like the default behavior, you can alter it with the -i switch, which will ask you to confirm your actions:

 $ ​​cp​​ ​​-i​​ ​​files/docs/markdown/chapter1.md​​ ​​files/docs/markdown/chapter4.md
 cp: overwrite 'files/docs/markdown/chapter4.md'? y

The cp command also accepts wildcard characters. That means you can copy all of the Markdown documents to a new directory with a single command. Let’s do that.

First, create a new directory called backups under the files directory:

 $ ​​mkdir​​ ​​files/backups

Then use the cp command to copy all of the Markdown files from the files/documents/markdown directory to the files/backups directory:

 $ ​​cp​​ ​​files/docs/markdown/*.md​​ ​​files/backups

Verify the files are in the files/backups directory using either the ls command or the tree command.

Copying Directories

When you copy a directory, you’re making a duplicate of the directory and its contents, including all files and subdirectories. Let’s demonstrate by making a backup of the code directory in our structure. Try copying files/code to files/backups/code:

 $ ​​cp​​ ​​files/code​​ ​​files/backups/code
 cp: -r not specified; omitting directory 'files/code'

It doesn’t work as expected because the cp command only copies files by default.

Try again, but this time, use the -r switch, which tells cp to recursively copy a directory and its contents:

 $ ​​cp​​ ​​-r​​ ​​files/code​​ ​​files/backups/code

This results in the following structure, which you can verify with the tree command:

 $ ​​tree​​ ​​files
 files
 ├── backups
 │   ├── chapter1.md
 │   ├── chapter2.md
 │   ├── chapter3.md
 │   ├── chapter4.md
 │   └── code
 │   ├── elm
 │   │   ├── main.elm
 │   │   └── README.md
 │   ├── go
 │   │   ├── main.go
 │   │   └── README.md
 │   └── js
 │   ├── app.js
 │   └── README.md
 ...

Notice that you specified the destination directory. If the destination directory doesn’t exist, the cp command creates it. However, run the same command again:

 $ ​​cp​​ ​​-r​​ ​​files/code​​ ​​files/backups/code

Now, check out the results. It’s probably not what you’d expect:

 $ ​​tree​​ ​​files/backups
 files/backups
 ...
 └── code
» ├── code
» │   ├── elm
» │   │   ├── main.elm
» │   │   └── README.md
» │   ├── go
» │   │   ├── main.go
» │   │   └── README.md
» │   └── js
» │   ├── app.js
» │   └── README.md
 ...

Rather than overwrite the files you just copied, the command placed a new copy of the code directory inside of the existing one.

If you don’t specify the destination directory, the cp command still creates the destination directory:

 $ ​​cp​​ ​​-r​​ ​​files/code/​​ ​​files/backups

Running this command again will produce the same results.

Of course, you can specify a new destination name entirely. Just keep in mind that if the destination directory you specify already exists, you’ll get a nested file structure.

Moving and Renaming Files and Directories

Filenames need to change, and things need occasional reorganization. You use the mv file to perform both of these operations. It works almost exactly like the cp command, except that the syntax is identical for moving files and directories. To move a file, you specify the source path and the destination path. Try it out.

First, rename the files/documents/markdown directory. Name it employee_handbook instead:

 $ ​​mv​​ ​​files/docs/markdown​​ ​​files/docs/employee_handbook

Next, create a manuals directory under files/docs and move the employee_handbook underneath it:

 $ ​​mkdir​​ ​​files/docs/manuals
 $ ​​mv​​ ​​files/docs/employee_handbook​​ ​​files/docs/manuals

That’s all there is to moving an entire directory structure. Moving individual files works the same way, and like the cp command, you can use wildcards to move multiple files.

Deleting Files and Directories

In the GUI, you delete files by dragging them to the Trash, the Recycle Bin, or some other metaphor for reclaiming disk space. On the command line, you typically skip that intermediate step and directly remove the files or directory.

To delete a file, you use the rm command. Let’s delete the chapter1.md file in the files/backups/ directory:

 $ ​​rm​​ ​​files/backups/chapter1.md

Like the other commands, you can use wildcards to delete multiple files. Delete the rest of the Markdown files in the files/backups directory:

 $ ​​rm​​ ​​files/backups/chapter*.md

To delete directories, you’ll have to use the -r switch to tell the rm command to delete recursively, even if there’s nothing in the directory. Let’s explore this by cleaning up the files/backups directory.

First, delete the files/backups/code directory and all of its contents:

 $ ​​rm​​ ​​-r​​ ​​files/backups/code

On Ubuntu and macOS, this deletes the entire directory and its files. On other operating systems, you may be prompted to confirm you want to delete each file and directory. If that’s the case, you can add the -f flag to the command:

 $ ​​rm​​ ​​-rf​​ ​​files/backups/code

Be Careful with rm

images/aside-icons/warning.png

The rm command is dangerous; there’s no easy way to undo it. You’ll have to resort to a backup. And rm -rf will forcibly delete files that are write-protected too. You’ll find lots of examples online that use rm -rf, but unless your OS prompts you to delete files you own, it’s safer to skip the -f flag. You can also use the -i flag and rm will ask you to confirm each deletion.

If you need to delete a bunch of files or directories in different locations, you can pass the paths to the rm command, and you can use brace expansion or wildcards as well. Let’s clean up some of the files we made. Run this command to delete the HTML and text files you made in your home directory, along with the style.css file and the text file you created in the Documents folder:

 $ ​​rm​​ ​​*.html​​ ​​style.css​​ ​​*.txt​​ ​​Documents/this_goes_in_Documents.txt

You can create, move, rename, and remove files and directories on any path on your system. Of course, unless you own the parent directory, you have to use sudo to perform those operations. But you can fix that if you change the permissions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.161.188