Basic Linux console commands
Just like navigating a desktop, it is important to be able to navigate a console.
This post is part of the "what is sudo?" series, which aims to help understand Linux and text consoles.
Dealing with files
Every folder in a system except the root folder is inside another folder. This other folder is the parent folder. The root folder, or top-most folder, is the folder that contains everything in your system.
The root folder is: /
.
Some users in your system have associated "home" folders. You can think of this folder as where a user would normally put their own "Documents", "Photos", etc.
Whenever you use a system, you must be logged in as a specific user. Let's find out what your current user is.
Open a console, and type the following command:
whoami
This will tell you the name of the user you are currently logged in as.
Next, lets navigate to your user's "home" folder:
cd ~
The cd
command performs a change directory command. The ~
folder is the current user's "home" folder.
Creating files and folders
To create a folder, we use the mkdir
command.
To create an empty file, we use the touch
command.
# This creates a folder 'documents' inside your home folder.
mkdir ~/documents
# This creates an empty file 'hello.txt' in the documents folder.
touch ~/documents/hello.txt
If we want to create a folder inside another folder, neither of which exist:
mkdir -p ~/documents/work/journal
This creates a folder called 'work', and then a folder called 'journal' inside it. If we omit the -p
, then it gives an error because it attempts to create the 'journal' folder inside a folder that doesn’t yet exist ('work').
Deleting files and folders
Finally, lets look at deleting. We delete using the rm
command.
# This deletes the file 'hello.txt'
rm ~/documents/hello.txt
# This deletes the folder 'work', including everything it contains
rm -rf ~/documents/work
The -rf
argument is required in order to delete folders. This is because we want to delete everything inside the folder, as well as the folder. The r
stands for "recursive", which tells the rm
command to go into each sub-folder and delete everything in it. The f
argument attempts to delete files even if they are not owned by the currently logged in user.
Review
whoami
shows the currently logged in user.cd [args]
changes directory to another directory.mkdir [-p] [args]
creates a directory.touch [args]
creates an empty file.rm [-rf] [args]
removes a file or directory.