You can navigate between files by clicking on them or moving across them using the much faster command line.
The Root Directory
The starting point for the file system is the root folder.
We call it the root, but its actual directory name is "/"
Confusingly, there is a sub-directory named "root". These are not the same!
/ refers to the root directory
The Home Directory
/home contains a home folder for each user on the system. For example, my home folder is located at /home/mustafa
It will contain all of the user’s files and directories.
~ refers to the home directory
pwd
The print working directory command is super simple but very useful. Think of it as a "where am I" command.
It will print the path of your present working directory, starting from the root /.
For example, if I were on my desktop and I ran
pwd
, I would see /home/mustafa/Desktop
ls
The list command will list the contents of a directory.
When used with no options or arguments, it prints a list of the files and folders located in the current directory.
We can also list the contents of a specific directory using
ls path
.For example,
ls /bin
will print the contents of the /bin directoryls # list contents of current directory ls /home/mustafa # list contents of /home/mustafa
cd
The cd command is used to change the current working directory, "moving" into another directory.
For example,
cd chickens
would change into the chickens directory (assuming it exists)First
ls
to list the directories and files and thencd
to move into one of themcd /home/mustafa
would take me to my home directorycd <directory>
Backing Up
In Unix-like operating systems, a single dot (.) is used to represent the current directory.
Two dots (..) represent the parent directory.
So we can use
cd ..
to move up one level, from our current directory into the parent directory.cd .. # "back up" one level into the parent folder
Relative Paths
When providing paths to commands like cd or ls, we have the option of using relative or absolute paths.
Relative paths are paths that specify a directory/file relative to the current directory.
For example, if our current directory is /home/mustafa and we want to cd into animals, we can simply run
cd animals
.However,
cd animals
does NOT work if we are located in another directory like /bin. The relative path from /bin is ../home/mustafa/animals
Absolute Paths
Absolute paths are paths that start from the root directory (they start with a / )
The absolute path to the animals directory is /home/mustafa/animals.
We can use absolute paths to specify a location no matter our current location.
For example, from the /bin directory I could use cd /home/mustafa/animals to change into the animals directory.
Creating Files and Folders
touch
To create a new file from the command line, use the
touch
command.Provide a filename, and that file will be created for you (assuming it doesn't already exist)
You can also provide a path where it has to create the file
For example,
touch chicken.txt
would create a chicken.txt file in the current directory.If you try to use touch with a file that already exists, it will simply update the access and modification dates to the current time.
You can also create multiple files by separating them with space
Don’t use spaces in filename, rather separate the words using a colon, underscore or use camelCase
touch <filename> touch public/index.html touch <filename> <filename> my-website my_website myWebsite
mkdir
To create new directories, we use the make directory(mkdir) command. We provide one or more directory names, and it will create them for us.
For example, to create two new folders, images and styles, we could run
mkdir images styles
mkdir <foldername>
Deleting, Copying and Moving
rm
We use the remove (rm) command to remove files from our machine.
For example,
rm app.js
would remove the app.js file.You can also delete multiple files at the same time by separating them with spaces and using paths to remove files in other directories
Note: rm DELETES FILES, there is no undo or recycling bin to retrieve them from! They are gone!
rm <filename>
To delete empty folders, we need to use the -d option with rm. For example,
rm -d cats
would remove the cats directory (only if it's already empty)To delete folders that are NOT empty, use the -r option which removes the directory and it’s contents recursively. For example, rm -r chickens would delete the chickens directory whether it's empty or not.
You want to be careful when deleting directories!
rm -r <foldername>
mv
Use the move command (mv) to move files and directories from one location to another.
When we specify a file or files as the source and a directory as the destination, we are moving the files into the directory.
For example,
mv app.css styles/
will move the app.css file into the styles directory.We can also use
mv
to move a directory to another directory.mv <source> <destination>
Renaming
We can also use the move command to rename files and folders.
If we specify a single file as the source and a single file as the destination, it will rename the file. For example, to rename the chickens.txt file to roosters.txt, we could run
mv chickens.txt roosters.txt
If we specify a single folder as the source and the destination doesn't yet exist, it will rename the folder. If the destination folder does exist, it will move our source folder into the destination.
mv <current> <newname>
cp
We can use the copy command to create copies of files and folders
To create a copy of sheep.txt called dolly.txt, we could run
cp sheep.txt dolly.txt
To copy multiple files into another directory, use
cp file1 file2 directory
.To copy a directory to another directory, you have to use the -r option.
cp <source> <destination>
Some Shortcuts Every Developer Should Know
Use ctrl-l to clear the entire screen
Use ctrl-a to move the cursor to the beginning of the line.
Use ctrl-e to move the cursor to the end of the line.
We can also use the
history
command to view the entire history
Often it's easiest to find an earlier command by searching using a small portion of the command that you remember.
Type ctrl-r to enter "reverse-i-search". As you start typing, bash will search the history and show you the best match.
Hit ctrl-r to cycle through potential matches.
Nano
Nano is a simple text editor that we can access right from the terminal. It's far more accessible than other popular command-line editors like Vim and Emacs.
Nano includes all the basic text editing functionality you would expect such as search, spellcheck, syntax highlighting, etc.
To open up a file using nano, run
nano FILE
. For example, to open up the file book.txt using nano, we would runnano book.txt
We can also use the same command to edit a file that doesn't yet exist (we can then save it and create the file).
We can use CTRL + S to save and CTRL + X to exit
nano filename