Linux: #2 - Command Line

Hussein Sarea

Hussein Sarea jul 26, 2022

14 min read 2795 words

1. The Shell

What is the shell? The shell is basically a program that takes your commands from the keyboard and sends them to the operating system to perform. If you’ve ever used a GUI, you’ve probably seen programs such as “Terminal” or “Console” these are just programs that launch a shell for you.

In this course we will use the shell program bash (Bourne Again shell), almost all Linux distributions will default to the bash shell. There are other shells available such as ksh, zsh, tsch, but we won’t get into any of those.

Let’s jump right in! Depending on the distribution your shell prompt might change, but for the most part it should adhere to the following format:

shell

username@hostname:current_directory
hussein@hussein-sarea:/home/hussein $

Notice the $ at the end of the prompt? Different shells will have different prompts, in our case the $ is for a normal user using Bash, Bourne or Korn shell, and # for superuser you don't add the prompt symbol when you type the command, just know that it's there.

Let’s start with a simple command, echo. The echo command just prints out the text arguments to the display.

shell

$ echo Hello World

2. pwd (Print Working Directory)

Everything in Linux is a file, as you journey deeper into Linux you’ll understand this, but for now just keep that in mind. Every file is organized in a hierarchical directory tree. The first directory in the filesystem is named the root directory. The root directory has many folders and files which you can store more folders and files, etc.

The location of these files and directories are referred to as paths. If you had a folder named home with a folder inside of it named hussein and another folder in that folder called Movies, that path would look like this: /home/hussein/Movies, pretty simple huh?

Navigation of the filesystem, much like real life is helpful if you know where you are and where you are going. To see where you are, you can use the pwd command, this command means print working directory and it just shows you which directory you are in, note the path stems from the root directory.

shell

$ pwd

3. cd (Change Directory)

Now that you know where you are, let’s see if we can move around the filesystem a bit. Remember we’ll need to navigate our way using paths. There are two different ways to specify a path, with absolute and relative paths.

  • Absolute path: This is the path from the root directory. The root is the head. The root directory is commonly shown as a slash. Every time your path starts with / it means you are starting from the root directory. For example, /home/hussein/Desktop.

  • Relative path: This is the path from where you are currently in filesystem. If I was in location /home/hussein/Documents and wanted to get to a directory inside Documents called taxes, I don’t have to specify the whole path from root like /home/hussein/Documents/taxes, I can just go to taxes/ instead.

Now that you know how paths work, we just need something to help us change to the directory we want to. Luckily, we have cd or change directory to do that.

shell

$ cd /home/hussein/Pictures

So now I've changed my directory location to /home/hussein/Pictures.

Now from this directory I have a folder inside called Screenshots, I can navigate to that folder with:

shell

$ cd Screenshots

Notice how I just used the name of the folder? It’s because I was already in /home/hussein/Pictures.

It can get pretty tiring navigating with absolute and relative paths all the time, luckily there are some shortcuts to help you out.

  • . (current directory). This is the directory you are currently in.
  • .. (parent directory). Takes you to the directory above your current.
  • ~ (home directory). This directory defaults to your “home directory”. Such as /home/hussein.
  • - (previous directory). This will take you to the previous directory you were just at.
shell

$ cd .

$ cd ..

$ cd ~

$ cd -
Tip #1

Give them a try!

4. ls (List Directories)

Now that we know how to move around the system, how do we figure out what is available to us? Right now it’s like we are moving around in the dark. Well, we can use the wonderful ls command to list directory contents. The ls command will list directories and files in the current directory by default, however you can specify which path you want to list the directories of.

shell

$ ls

$ ls /home/hussein

ls is a quite useful tool, it also shows you detailed information about the files and directories you are looking at.

Also note that not all files in a directory will be visible. Filenames that start with . are hidden, you can view them however with the ls command and pass the -a flag to it (a for all).

shell

$ ls -a

There is also one more useful ls flag, -l for long, this shows a detailed list of files in a long format. This will show you detailed information, starting from the left: file permissions, number of links, owner name, owner group, file size, timestamp of last modification, and file/directory name.

shell

$ ls -l

5. touch

Let’s learn how to make some files. A very simple way is to use the touch command. Touch allows you to the create new empty files.

shell

$ touch random_file

And boom, new file!

If you want to create a file which contain spaces with its name you will have to use "".

shell

$ touch "random file"
Tip #2

Touch is also used to change timestamps on existing files and directories. Give it a try, do an ls -l on a file and note the timestamp, then touch that file and it will update the timestamp.

There are many other ways to create files that involve other things like redirection and text editors, but we’ll get to that in other tutorial.

6. file

In Linux, filenames aren’t required to represent the contents of the file. You can create a file called funny.gif that isn’t actually a GIF unlike other Operating System.

To find out what kind of file a file is, you can use the file command. It will show you a description of the file’s contents.

shell

$ file Nuxt-logo.webp

7. cat

We’re almost done navigating files, but first let’s learn how to read a file. A simple command to use is the cat command, short for concatenate, it not only displays file contents but it can combine multiple files and show you the output of them.

shell

$ cat "random file" f2.txt
Warning

It’s not great for viewing large files and it’s only meant for short content. There are many other tools that we use to view larger text files that we’ll discuss in the next section.

8. less

If you are viewing text files larger than a simple output, less is more. (There is actually a command called more that does something similar, so this is ironic.) The text is displayed in a paged manner, so you can navigate through a text file page by page.

Go ahead and look at the contents of a file with less. Once you’re in the less command, you can actually use other keyboard commands to navigate in the file.

shell

$ less /home/hussein/Documents/textFile

Use the following command to navigate through less:

  • q Used to quit out of less and go back to your shell.
  • Page up, Page down, Up and Down - Navigate using the arrow keys and page keys.
  • g Moves to beginning of the text file.
  • G Moves to the end of the text file.
  • /search - You can search for specific text inside the text document. Prefacing the words you want to search with /
  • h If you need a little help about how to use less while you’re in less, use help.

9. history

The history command in Linux is a built-in shell tool that displays a list of commands used in the terminal session. history allows users to reuse any listed command without retyping it.

shell

$ history

Note Want to run the same command you did before, just hit the up arrow.

Another history shortcut is ctrl-R, this is the reverse search command, if you hit ctrl-R and you start typing parts of the command you want it will show you matches and you can just navigate through them by hitting the ctrl-R key again. Once you found the command you want to use again, just hit the Enter key.

Our terminal is getting a little cluttered. Let’s do a little cleanup, use the clear command to clear up your display.

shell

$ clear

10. cp (Copy)

Let’s start making some copies of these files. Much like copy and pasting files in other operating systems, the shell gives us an even simpler way of doing that.

shell

$ cp hussein.jpg /home/hussein/Pictures/

In that example hussein.jpg is the file you want to copy and /home/hussein/Pictures/ is where you are copying the file to.

You can copy multiple files and directories as well as use wildcards. A wildcard is a character that can be substituted for a pattern based selection, giving you more flexibility with searches. You can use wildcards in every command for more flexibility.

  • * the wildcard of wildcards, it's used to represent all single characters or any string.
  • ? used to represent one character.
  • [] used to represent any character within the brackets.
shell

$ cp *.jpg /home/hussein/Pictures/jpg-images

This will copy all files with the .jpg extension in your current directory to the Pictures directory.

To copy a directory you have to use -r flag.

shell

$ cp -r MyPdfs/ /home/hussein/Documents

One thing to note, if you copy a file over to a directory that has the same filename, the file will be overwritten with whatever you are copying over.

You can use the -i flag (interactive) to prompt you before overwriting a file, so that you don't accidentally overwrite your files.

shell

$ cp -i hussein.jpg /home/hussein/Pictures

11. mv (Move)

Used for moving files and also renaming them. Quite similar to the cp command in terms of flags and functionality.

You can rename files like this:

shell

$ mv hussein.jpg me.jpg

Or renaming files using absolute path.

shell

$ mv /home/hussein/Pictures/hussein.jpg /home/hussein/Pictures/me.jpg

Or you can actually move a file to a different directory:

shell

$ mv hussein.jpg /home/hussein/my-images

And move more than one file:

shell

$ mv hussein.jpg moataz.jpg /home/hussein/Pictures

You can rename directories as well:

shell

$ mv my-images myImages

Like cp, if you mv a file or directory it will overwrite anything in the same directory. So you can use the -i flag to prompt you before overwriting anything.

shell

$ mv -i directory1 directory2

Let’s say you did want to mv a file to overwrite the previous one. You can also make a backup of that file and it will just rename the old version with a ~.

shell

$ mv -b directory1 directory2

12. mkdir (Make Directory)

We’re gonna need some directories to store all these files we’ve been working on. The mkdir command (Make Directory) is useful for that, it will create a directory if it doesn’t already exist. You can even make multiple directories at the same time.

shell

$ mkdir codes github-repos

You can also create subdirectories at the same time with the -p (parent flag).

shell

$ mkdir -p codes/cpp/algorithms

13. rm (Remove)

Now I think we have too many files, let’s remove some files. To remove files you can use the rm command. The rm (remove) command is used to delete files and directories.

shell

$ rm random_file

Take caution when using rm, there is no magical trash that you can fish out removed files. Once they are gone, they are gone for good, so be careful.

Fortunately there are some safety measures put into place, so the average joe can’t just remove a bunch of important files. Write-protected files will prompt you for confirmation before deleting them. If a directory is write-protected it will also not be easily removed.

If you don’t care about any of that, you can absolutely remove a bunch of files.

shell

$ rm -f random_file

-f or force option tells rm to remove all files, whether they are write protected or not, without prompting the user (as long as you have the appropriate permissions).

shell

$ rm -i file

Adding the -i flag like many of the other commands, will give you a prompt on whether you want to actually remove the files or directories.

shell

$ rm -r directory

You can’t just rm a directory by default, you’ll need to add the -r flag (recursive) to remove all the files and any subdirectories it may have.

You can remove a directory with the rmdir command.

shell

$ rmdir directory

14. find

find is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

shell

$ find /home -name hussein.jpg

With find you’ll have to specify the directory you’ll be searching it, what you’re searching for, in this case we are trying to find a file by the name of hussein.jpg.

One cool thing to note is that find doesn’t stop at the directory you are searching, it will look inside any subdirectories that directory may have as well.

15. help

Linux has some great built-in tools to help you how to use a command or check what flags are available for a command. One tool, help, is a built-in bash command that provides help for other bash commands (echo, logout, pwd, etc).

shell

$ help echo

This will give you a description and the options you can use when you want to run echo.

For other executable programs, it’s convention to have an option called --help or something similar.

shell

$ echo --help

Not all developers who ship out executables will conform to this standard, but it’s probably your best shot to find some help on a program.

16. man

Man pages are manuals that are by default built into most Linux operating systems. They provide documentation about commands and other aspects of the system.

$ man ls
Tip #3

Try it out on a few commands to get more information about them.

17. whatis

if you are ever feeling doubtful about what a command does, you can use the whatis command. The whatis command provides a brief description of command line programs.

shell

$ whatis cat

18. alias

Sometimes typing commands can get really repetitive, or if you need to type a long command many times, it’s best to have an alias you can use for that. To create an alias for a command you simply specify an alias name and set it to the command.

shell

$ alias foobar='ls -la'

Now instead of typing ls -la, you can type foobar and it will execute that command, pretty neat stuff. Keep in mind that this command won't save your alias after reboot, so you'll need to add a permanent alias in:

shell

~/.bashrc

or similar files if you want to have it persist after reboot.

You can remove aliases with the unalias command:

shell

$ unalias foobar
$ unalias -a [remove all alias]

19. exit

To exit from the shell, you can use the exit command

shell

$ exit

Or the logout command:

shell

$ logout

Or if you are working out of a terminal GUI, you can just close the terminal, see you in the next article!

Share on Social Media: