Now that we're into the shell, lets learn some basic commands. To run a command, type it out and press enter.
This stands for print working directory. Your working directory is where your current commands will be executed: for instance, if I ran a command to create a file and my working directory was /home/hackbu/, the created file's full path would be /home/hackbu/<filename>.
ls is an abbreviation for list. When you just run ls, it lists the files in your current working directory. If you add the path of a different directory as a parameter (see below), it lists the files in that directory instead. ls also offers various flags to format and control the output.
cp is short for copy. It takes two arguments, the source and the destination, and makes a copy of the source at the destination. For instance, cp myfile myfolder/mycopy creates a new, separate file at myfolder/mycopy with the same contents.
cd is an abbreviation for change directory. It takes one parameter, the path to the new directory.
There are two types of path: relative and absolute paths.
- An absolute path starts with a
/, and does not depend on your current working directory. - A relative path does not start with a
/and depends on your current working directory.
Consider the two following examples:
Documents/Code/home/<username>/Documents/Code
If your current working directory is already /home/<username>, then the two paths are equivalent.
Additionally, there are a few special paths:
.means the current path. So, if we are in/home/<username>/myfolder, the path./myfileis the same as the absolute path/home/<username>/myfolder/myfile...means the parent directory, or the directory one level "up". So, if we are again in/home/<username>/myfolder, the paths.and../myfolderare equivalent, and..and/home/<username>are equivalent.~means the current user's home folder, usually at/home/<username>
cd accepts one further special path: when you run cd -, you will jump back to the previous working directory.
mkdir is an abbreviation for make directory. It takes one argument, the path to the directory to create.
rm is an abbreviation for remove. When you run rm <myfile>, <myfile> is PERMANENTLY DELETED. By default there is no way to recover files deleted with rm. Be very careful using it!
If you want to delete a directory, you must add the flag -r, as in rm -r <myfolder>. However, rm will refuse to delete a directory with files inside it. We commonly use rm -rf <myfolder> to delete a non-empty directory.
You might see
rm -rf /as something of a meme—this command would delete your entire filesystem and all your files!
cat is an abbreviation for concatenate, as in we concatenate the file's content to our current terminal's content. We often use cat to read the contents of short text files. cat <myfile> will display the contents of <myfile>. Useful!
The last command we need to get started is touch. When we run touch <myfile>, one of two things should happen:
- if
<myfile>already exists, its "last modified" timestamp will be updated - if
<myfile>does not exist, it is created as an empty file
(Skip if you don't care about details for the moment):
There are two kinds of commands above. Most of them are scripts or programs: separate programs that Bash runs behind the scenes when you call them. However, there are also builtins: commands that are part of Bash. For technical reasons that you will learn about in your classes, cd must be implemented as a shell builtin and not a command.
You can check whether a command is a program or a builtin by running type <commandname>.
We can control many commands by appending more text. Technically, every "word" in your command is a separate parameter, but people often refer to arguments and flags. A flag is usually preceded by two hyphens, as in ls --all, and often has an abbreviated form, as in ls -a. Arguments are the other words appended to the command, like <myfolder> in cd <myfolder>.
Phew, that's quite a lot to start! Let's put it all into action.
Fire up your Bash shell.
- Run
pwd. Where does your shell start? - Run
ls. What do you see? - Run
ls --all. How is the output different?
On Unix devices, files whose names start with a period, like .hiddenfile, are hidden by default. Note also that the special paths . and .. now appear!
Now, lets make a folder.
- run
mkdir myfolder - run
ls. What do you see? - run
pwd, then runcd myfolderand runpwdagain to see how your working directory has changed.
Your newly-created folder should be empty. Let's create a file!
- run
touch myfile, then runls - run
cat myfile.touchcreates empty files, so you shouldn't see anything. - run
echo "hello world!" >myfile(don't worry about what that means for now). - run
cat myfileagain. - run
cp myfile mycopy. - run
cat mycopy.
Almost done!
- Go back to the parent directory by running
cd -orcd ..Why are these the same? - run
ls myfolder: we can list the contents of directories other than our current one! - It's time to clean up. Run
rm myfolder. - Since that didn't work, let's try
rm -r myfolder - In all likelihood, that failed too! Carefully run
rm -rf myfolder, and finish off with anlsto make sure it's gone.