Interacting with Files
In previous sections we learned how to look at files and directories in our system. Now we will learn how to create, delete, rename and move them around.
Making a Directory
Creating a directory is pretty easy. The command we are after is mkdir
which is short for make directory. In it’s most basic form we can run mkdir
supplying only a directory and it will create it for us.
Terminal
$ pwd
/home/student
$ ls
anaconda3 Documents DSIB01demo Pictures snap Videos
Desktop Downloads Music Public Templates
$ mkdir test_dir
$ ls
anaconda3 Documents DSIB01demo Pictures snap test_dir
Desktop Downloads Music Public Templates Videos
Remember that when we supply a directory in the above command we are actually supplying a path. In this case, we were using relative path.
There are a few useful options available for mkdir
. The most used one is -p
which tells mkdir
to make parent directories as needed.
Terminal
$ pwd
/home/student
$ mkdir test_dir/foo/bar
$ cd test_dir/foo/bar
$ pwd
/home/student/test_dir/foo/bar
To see other options, you can look into man pages.
Good names for Files and Directories
Complicated names of files and directories can make your life painful when working on the command line. Here we provide a few useful tips for the names of your files and directories.
1) Don’t use spaces. Spaces can make a name more meaningful, but since spaces are used to separate arguments on the command line it is better to avoid them in names of files and directories. You can use -
or _
instead (e.g. north-pacific-gyre/
rather than north pacific gyre/
). To test this out, try typing mkdir north pacific gyreand
see what directory (or directories!) are made.
2) Don’t begin the name with -
(dash). Commands treat names starting with -
as options.
3) Stick with letters, numbers, .
(period or ‘full stop’), -
(dash) and _
(underscore). Many other characters have special meanings on the command line. There are special characters that can cause your command to not work as expected and can even result in data loss.
If you need to refer to names of files or directories that have spaces or other special characters, you should surround the name in quotes (""
).
Removing a Directory
The command to remove a directory is rmdir
, short for remove directory. This will remove an empty directory and it also support a -p
option to remove parent directories as well. One thing to note, however, is that there is no undo when it comes to the command line on Linux (Linux GUI desktop environments typically do provide an undo feature but the command line does not). Just be careful with what you do.
Terminal
$ cd
$ pwd
/home/student
$ ls test_dir/foo
bar
$ rmdir test_dir/foo/bar
$ ls test_dir/foo
Creating a File
One way to create a file is to open it in a text editor - text editor will create this file if doesn’t exist. Let’s use text editor called nano
.
Terminal
$ cd test_dir
$ nano draft.txt
Let’s type in a few lines of text. Once we’re happy with our text, we can press Ctrl
+O
to write our data to disk (we’ll be asked what file we want to save this to: press Return
to accept the suggested default of draft.txt
). Once our file is saved, we can use Ctrl
+X
to quit the editor and return to the shell.
nano
doesn’t leave any output on the screen after it exits, but ls
now shows that we have created a file called draft.txt
Terminal
$ ls
draft.txt
Moving Files and Directories
The command we use for this is mv
which stands for move. mv
takes to parameters: source and destination. Note that both the source and destination are paths. This means we may refer to them using both absolute and relative paths.
Terminal
$ mv draft.txt foo/
$ ls foo
draft.txt
When destination argument is directory, specified file is moved into this directory and its name is not changed.
We might also use mv
for renaming files. We specify destination as part to a file. One must be careful when specifying the target file name, since mv will silently overwrite any existing file with the same name, which could lead to data loss. An additional option, mv -i
(or mv --interactive
), can be used to make mv ask you for confirmation before overwriting.
Terminal
$ mv foo/draft.txt foo/text.txt
$ ls foo
text.txt
Copying Files and Directories
The cp
command works very much like mv
, except it copies a file instead of moving it. We can check that it did the right thing using ls
with two paths as arguments — like most Unix commands, ls can be given multiple paths at once.
Terminal
$ cp foo/text.txt notes.txt
$ ls foo/text.txt notes.txt
foo/text.txt notes.txt
We can also copy a directory and all its contents by using the recursive option -r
, e.g. to back up a directory.
Terminal
$ cp -r foo backup_foo
$ ls foo backup_foo
backup_foo:
text.txt
foo:
text.txt
Removing Files and Directories
As with rmdir
, removing a file is an action that may not be undone so be careful. The command to remove or delete a file is rm
which stands for remove.
Terminal
$ rm notes.txt
$ ls
foo backup_foo
rm
can remove a directory and all its contents if we use the recursive option -r
, and it will do so without any confirmation prompts.
Terminal
$ rm backup_foo
$ ls
foo
Given that there is no way to retrieve files deleted using the shell, rm -r
should be used with great caution (you might consider adding the interactive option rm -r -i
).
Previous page | Home | Next page |