I’m currently in the process of studying for the RHCSA exam. As such, I’m learning a whole bunch of commands in Linux I either haven’t used before or haven’t used in a long time.
I have to admit – I’m a bit old school. I love working on the command line. Seeing that little blinking cursor just waiting for me to type some commands, fills me with joy! If I had started using Linux later in life, I’d probably prefer the GUI interface more or fear the command line. The command line may seem intimidating at first, but once start using it, you may like it as much as I do!
What is the command line?
The Linux command line is just a text interface to your computer. It has many names such as terminal, console, prompt and at first glance, it tends to scare people. In the early years, when Linux didn’t exist yet, one of the earliest operating systems was Unix. It was designed to run as a multi-user system and people connected to it remotely by using terminals. Terminals were pretty basic with no mouse or fancy graphics. A terminal with a keyboard was all that was needed to send keystrokes to the mainframe and receive the text back. People had to manage their files by using commands. Instead of going to the “File Explorer” to create a new folder or rename a file like you would in Windows, you use commands to change directories (cd) and list (ls) files.
Let’s get started!
I am currently using the Fedora operating system. You can launch the terminal by clicking on the Activities menu at the top left of the screen and then typing “terminal”, “command”, “prompt’“, or “shell”.
Now you are at the terminal with a blinking cursor – just asking you to feed it some commands!
The important thing to remember when working with Linux is that Linux is a case-sensitive operating system. That means if you type ls it is different than typing LS:
Click inside the terminal with your mouse and type pwd and then press Enter.
You should see the current directory that you are in:
The pwd command displays the name of the current/working directory that you are in. Your working directory is the current directory that you are in and when you run a command, such as creating a file, it will assume you want it to be created in the directory that you are currently in. The pwd command will tell you exactly what directory you are currently in.
Note: When you run a command, it will display the output of the command directly in the terminal and then you will be shown the prompt again once the command is finished running. Some commands can product a lot of text output and some commands will run silently and not display any output at all.
You can change your working directory by using the cd command. If you type cd one, you will move to the directory called ‘one’ in your home directory. If you want to go back one directory, just type cd, press the spacebar, and then type .. and then press the Enter key.
If you ever need to return to your home directory, you can use the hand shortcut – the tilde (~) character. Go ahead and try changing directories and seeing how the cd command works. You can always use pwd to display the current directory that you are in and cd ~ to get back to your home directory:
Now, let’s start viewing the files that are in the directories.
If you just type ls at the command line, you will see a listing of your files and directories in your current working directory:
You can also list files recursively with the -R switch:
Linux also has the concept of hidden files – files that are not automatically displayed when listing directory contents. Hidden files start with a . and are usually system or application files, hidden to prevent accidental changes. You can display them by typing ls -a
Creating folders and files
You can create folders by using the mkdir command. You can make one directory at a time or several at once.
You can create files multiple ways. You can use the touch command to create a file with nothing in it:
You can also use redirection to create a file. For example, if you wanted to capture the output of a certain command, you can do that by running the command and then using the greater-than symbol (>) to write the output to a file:
No output gets printed to the screen because the output was sent to the lsoutput.txt file. You can view the contents of the lsoutput.txt file by using the cat command:
The cat command concatenates and prints files. As you can see, it displays all of the same data you would see if you had just typed ls.
This has just been a brief introduction to the command line and a few common commands that you will use when working at the command line. The goal here was to get you familiar with the command line and see that it isn’t something to be feared. I will demonstrate more commands in future blog posts.