DEV Community

Cover image for 17 commands to master the Linux Command Line
Fullstack Dev
Fullstack Dev

Posted on

17 commands to master the Linux Command Line

The Shell

The shell is a program (Terminal) that takes commands from the user.
The default shell program on almost all the Linux distributions is the Bourne Again shell also known as Bash.

You can find other shells available such as

  • Korn Shell ksh
  • Z shell zsh
  • TENEX C shell tcsh

For most distributions, the shell prompt should have this format:

username@hostname:current_directory $ 
Enter fullscreen mode Exit fullscreen mode

The $ is there by default and doesn't need to be added.

$ echo Hello from bash
$ date 
$ whoami
Enter fullscreen mode Exit fullscreen mode

The echo command prints the string Hello from bash to the display.

The date will print the date and the whoami will print your username

Print working directory

Linux like every other Unix-based system has its files organized in a hierarchical structure and acts as a blueprint.

/

|-- bin

|   |-- file1

|   |-- file2

|-- etc

|   |-- file3

|   `-- directory1

|       |-- file4

|       `-- file5

|-- home

|-- var
Enter fullscreen mode Exit fullscreen mode

The root directory / contains a number of folders and file and allows you to store more folders and files.

Each of the location of these folders and files are called paths.

For example a folder named home that contains a folder named projects its path would look like this /home/projects

You can use the pwd command to which directory you are in.

change directory

cd means change directory. It is used to navigate from one directory to another in the filesystem.

There are two types of paths:

  1. Absolute path, which means, this path starts from the root /.
/home/pictures/vacations
Enter fullscreen mode Exit fullscreen mode
  1. Relative path, which means, this path starts from the directory you are in. For example let's say you are in /home/pictures/vacations and you want to get inside vacations to a folder named Italy
username@hostname:/home/pictures/vacations $ cd Italy
Enter fullscreen mode Exit fullscreen mode

In other words, we're navigating from our current directory to the "Italy" folder.

. (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/pictures.
- (previous directory). This will take you to the previous directory you were just at.

list directories

You want to know what folders and files are available in a certain directory, you will need this command ls.

You can also specify the path you want to list the directories of

$ ls /home/pictures/vacations
Enter fullscreen mode Exit fullscreen mode

Note that files starting with . will not be visible to us using the ls command but we can pass the -a flag to see them

$ ls -a 
Enter fullscreen mode Exit fullscreen mode

Another helpful ls flag is -l for long format, which displays the list of files in a long format. beginning on the left: file size, last modification time, owner name, owner group, number of links, file permissions, and file/directory name.

you can combine both flags to have the same combined results ls -la

Here are some useful flags:

$ ls -r
$ ls -t
Enter fullscreen mode Exit fullscreen mode
  • ls -r Lists the current directory's content in reverse order.
  • ls -t Lists and sorts the current directory's content by modification time starting by the newest.

touch command

The touch command is used to create empty files.

$ touch my_new_file
Enter fullscreen mode Exit fullscreen mode

The touch command will also modify the modification and access time to the current system time.

  • -a: Updates only the access time.
  • -c: Updates only the modification time
  • -t: Allows setting a specific date and time for the timestamps.

You can use file name_of_file to get a description of the file's content.

The cat command allows you to display the content of a file $ cat file_name
You can also combine multiple files and show you the output $ cat file_name1 file_name2

Less

The less command opens the file in the terminal
you can press q to quit.

  • g moves to the beginning of the file
  • G moves to the end of the file
  • -N displays line numbers
  • /search is used to search for a specific text inside the file

Viewing all the previous commands

$ history
Enter fullscreen mode Exit fullscreen mode

This command is useful to see all the commands you previously used. You can also use this shortcut !! instead.

$ !!
Enter fullscreen mode Exit fullscreen mode

You can hit the up arrow to go back to the previous command instead of typing it again

if you are not sure about a certain command or remember a part of a command you can type CTRL + R and start typing a part of the command you want and a match will become visible to you.

You can also clear the terminal by writing clear

$ clear
Enter fullscreen mode Exit fullscreen mode

or you can type CTRL + l

Copy, move, make and remove files and directories.

By giving the name of the file and the path to the location you wish to copy it to, you can copy a single file.

$ cd name_of_file /home/myFiles/newDocs
Enter fullscreen mode Exit fullscreen mode

You can copy several files and folders and use wildcards. A wildcard is a character that can increase the flexibility of your search by substituting it for a pattern-based choice. Wildcards are additionally versatile as they can be used in any command.

  • * it's used to represent all single characters or any string.
  • ? used to represent one character
  • [] used to represent any character within the brackets
$ cp *.txt /home/Documents/videos
Enter fullscreen mode Exit fullscreen mode

this will copy all the files with the .txt extension in the current directory to the videos directory.

Using the -r flag when running a command can be helpful since it copies files and directories within a directory recursively.

$ cp -r vacation/ /home/Documents/videos
Enter fullscreen mode Exit fullscreen mode

If you copy a file to a directory that has the same filename, the file will be overwritten with whatever you're copying over. You can use the -i flag to avoid that.
The -i flag will prompt you to confirm if you wanna overwrite a file before you overwrite it.

cp -i myfile /home/Documents/
Enter fullscreen mode Exit fullscreen mode

moving files and directories

The mv command allows you to move a file to a different directory by taking the file's name and the location to where you want it moved.
This also works for directories.

$ mv file1 /home/Documents
$ mv myfolder /home/documents
Enter fullscreen mode Exit fullscreen mode

You can also move multiple files by providing the names of the files and the path to where you want them moved.

$ file1 file2 /home/Documents
Enter fullscreen mode Exit fullscreen mode

You can also use the -i flag to prompt a confirmation before overwriting anything.

we can also create a backup of a directory using the -b flag

$ mv -b myfolder myfolder_backup
Enter fullscreen mode Exit fullscreen mode

You can also use the mv command to rename files like so

$ mv myfile new_file
Enter fullscreen mode Exit fullscreen mode

making a new directory and removing a directory

You can use the mkdir command to create a directory.

$ mkdir folder folder1 
Enter fullscreen mode Exit fullscreen mode

you can also use the -p flag to create subdirectories at the same time

$ mkdir -p folder/folder1/folder2
Enter fullscreen mode Exit fullscreen mode

You can remove files using the rm command

$ rm myfile
Enter fullscreen mode Exit fullscreen mode

Using rm doesn't mean the removed files will be in the trash. Once this command is used the files will be completely gone.

You can use the -f flag to force remove the files even if they're write-protected ( you can't rename, move, or delete its parent directory.)

You can also use the -i flag to prompt for confirmation.

You can't use the rm on its own to remove a directory, you need to use the -r flag. Using this flag will allow the removal of all the files and any subdirectories it has.

$ rm -r myfolder
Enter fullscreen mode Exit fullscreen mode

You can remove a directory by using rmdir command

$ rmdir folder1
Enter fullscreen mode Exit fullscreen mode

Find a specific file

You can use the find command if you're trying to find a specific file. You need to specify the directory you'll be searching and what you're searching for. It also looks inside subdirectories.

$ find /home -name coffee.jpg
Enter fullscreen mode Exit fullscreen mode

You can also specify the type of file you are trying to find.

$ find /home/Documents -type d -name videos
Enter fullscreen mode Exit fullscreen mode

Manual

If you need some information about a specific command you can use the man or whatis command.

$ man ls
$ whatis cp
Enter fullscreen mode Exit fullscreen mode

Alias

You can give an alias to repetitive commands instead of typing them especially if they're long ones.
To create an alias:

alias la= 'las -la`
Enter fullscreen mode Exit fullscreen mode

Now you can type la instead of ls -la command and you will have the same output.

If you want to remove the alias you can use

$ unalias la
Enter fullscreen mode Exit fullscreen mode

Check flags

If you wanna know what flags are available for a certain command you can use help

$ ls --help
Enter fullscreen mode Exit fullscreen mode

Feel free to leave a question in the comment section!

Top comments (0)