Command line file management
- Looking around
- Making a directory
- Navigating directories
- Moving and renaming directories
- Removing directories
Being connected to a remote computer is nice, but we really want to be able to actually do stuff on that remote computer. The very first thing we’re going to do is to re-do something that we did when organizing our files: we’re going to navigate around folder structures and create new folders, ultimately to re-create the folder structure you created in the last unit.
Looking around
When you connect to a remote computer you’re interacting with a program called a “shell”. When you see something that looks like
[you@bird ~]>
the shell is patiently waiting for you to type some commands into it to tell it what to do.
Let’s start by running a command that will ask the shell to print out the contents of the current working directory. Note: the words “directory” and “folder” are synonyms for each other, so any time someone is talking about a directory, they mean folder.
The ls command asks the shell to print out the files and
folders that are in the current directory.
Type ls into your terminal, then press
Enter:
lsbin Mail
The folders that you see here were created for you when your account on the remote computer was created. What you see may not be exactly the same as what is shown here.
Making a directory
While bin and Mail are good directories to
have, they don’t represent what we want to actually keep on our remote
computer, so let’s start making some of our own folders.
We’re going to start small, let’s make a new folder. When we made
folders with our visual file explorers we mostly used our mice, though
you may have used a keyboard shortcut. While you can literally click on
your terminal window, clicking on the terminal window isn’t actually
going to do anything other than making a fun clicking sound from your
mouse (if you’re literally using a mouse) or a faint tapping sound as
you tap your touchpad. Instead, we’re going to need to use a new command
to create a folder: mkdir.
The mkdir command will create a new folder that has the
name that you pass to it on the command line.
Type mkdir hello into your terminal, press
Enter, then re-run ls:
mkdir hello
lsbin hello Mail
You just made a new folder 🎉!
One (minor) problem with making folders in our terminal is that we
can’t use spaces ' ' in the same way that we could when
making folders in our visual file explorers:
mkdir hello world
lshello world
This didn’t make one folder named hello world, it made
two folders, one named hello and one named
world. You’ve got a few options:
Don’t use spaces at all and instead use dashes
-or underscores_to represent spaces, or just don’t have spaces (similar to naming variables).mkdir hello-world mkdir hello_world mkdir helloworldPut quotes around the name that includes spaces.
mkdir "hello world" mkdir 'hello world'Escape the spaces with a backslash character
\.mkdir hello\ world
The issue with spaces is common to all commands on the command line. This is a good opportunity to choose a new personal preference: When you’re working on the command line, do you want to always use dashes, underscores, or no spaces at all?
Moving and renaming directories
Sometimes when making a directory you realize that you made the directory in the wrong place, or you gave it the wrong name (or misspelled the name).
You can rename or move a directory from one place to another (or both
at the same time!) with the mv command.
You can rename a directory with mv.
lsbin hello Mail
We should see a directory named hello because of the
mkdir we ran before.
mv hello not-hello
lsbin Mail not-hello
You can move a directory into another directory with
mv, too.
# intentionally making two directories
mkdir hello world
lsbin Mail hello world
mv hello world/ # move hello *into* world/
cd world # change into world
ls # now hello is *in* world/hello
ls .. # hello is *not* in ~bin Mail world
Just like with cd, you can use the special directories
.. and ~ to move (mv) a file into
the parent directory, list (ls) files in the parent
directory, or move (mv) files into your user directory (or
list your user directory with ls ~). You can also use
another special directory name . (one period) to move
something to or from the current directory.
You can move a directory to or from your home directory with
~ and use the . directory to move things into
or from the current directory.
cd world
mv hello ~ # move hello to your user directory
lsThere should be no output, we just moved hello out of
the directory.
# move hello back into *this* directory
mv ~/hello .
lshello
Removing directories
Sometimes you don’t need a directory anymore and you want to remove
it. You can remove directories using two different commands:
rm and rmdir.
You can remove empty directories using the
rmdir command, and this is usually the command you
want to use to try removing a directory.
rmdir helloIf you try to remove a directory that has other stuff in it (files or
folders), rmdir will warn you and refuse to remove the
directory:
mkdir hello
# makes a directory in hello
# named world
mkdir hello/world
rmdir hellormdir: failed to remove directory 'hello': Directory not empty
You can remove files and directories using the rm
command, and you can remove directories that have things in them by
asking rm to remove recursively.
rm -r hello # remove hello and all of its contentsThe rm command itself will refuse to remove directories
unless you tell it to remove them recursively.
rm hellorm: cannot remove 'hello': Is a directory
The command line doesn’t have a concept of a “temporary trash location” like Windows (the recycle bin) or macOS (the trash can). When you put something into the recycle bin or trash can, you can always open the recycle bin or trash can and get the file back, restoring it to its original location.
When you remove a file or folder on the command line (using
rm or rmdir), the file or folder is removed
and you can’t get it back, unless you’ve backed it up somewhere.