18 essential commands for new Linux users

Getting started on the Linux command line might seem overwhelming at first, but the many commands you need to use will fall into place more quickly than you might imagine. If you are just getting started with Linux, try each of the commands described below.

Once you’ve opened a terminal window, press the Enter key a few times and notice the prompt that is displayed. It might be just a $, but might include your username and the hostname of the system you’ve logged into as shown in the examples below.

1. ls

One of the first commands you will need to know is the ls command, which lists files and directories. If this is your first login session, you may see no output. That doesn’t mean there aren’t any files. It just means that you’ll have to work a little harder to see them. The ls command assumes that you don’t want to see files that start with dots, and any files that are included in a new home directory, like .bashrc, start with dots. You can use the -a option with ls to see files with names that start with a “.” (the only files that will exist in a home directory that was just set up). Next, try the ls command with the -al options (ls -al) to list files with details. Here’s an example of that command and its output:

[jdoe@fedora ~]$ ls -al
total 12
drwx------. 1 jdoe jdoe 92 Apr 3 14:15 .
drwxr-xr-x. 1 root root 26 Apr 3 14:14 ..
-rw-r--r--. 1 jdoe jdoe 18 Aug 11 2024 .bash_logout
-rw-r--r--. 1 jdoe jdoe 144 Aug 11 2024 .bash_profile
-rw-r--r--. 1 jdoe jdoe 522 Aug 11 2024 .bashrc
drwx------. 1 jdoe jdoe 0 Apr 3 14:15 .cache
drwxr-xr-x. 1 jdoe jdoe 34 Oct 24 10:49 .mozilla

The .bash_profile and .bashrc files help configure your login session by doing things such as setting up the your default search path – enabling you to run most commands without having to include the full path. For example, you can type “date” instead of “/usr/bin/date” because your shell will look through the directories included in your search path.

You’ll need to become familiar with the permissions strings that show that whether an entry is a file (first character in the listing is a hyphen) or a directory (first character in the listing is a “d”). In the listing shown above, the owner has read and write permissions for the three files while the user’s group and everyone else with an account on the system has only read permission.

To list permissions on your home directory (not its contents), use a command like this that shows no one else has any access permissions:

[jdoe@fedora ~]$ ls -ld /home/jdoe
drwx------. 1 jdoe jdoe 106 Apr 3 14:39 /home/jdoe

As you may have suspected, “r” stands for read, “w” means write and “x” is for execute. Note that no permissions are available for other group members and anyone else on the system. Each user will be the only member of a group with the same name. For example, the user “jdoe” is the only member of the group “jdoe”, but could be a member of other groups as well.

It’s important to know that files in other locations on the system can be listed by using the full paths names provided, of course, that you have read access to the directories in which the files are located.

[jdoe@fedora ~]$ ls /usr/bin/date
/usr/bin/date

Read more: Linux fundamentals: Viewing files, commands, processes and systems

2. which

The which command will provide the full path name for a command. It does this by searching through all the directories in your search path until it finds it and then displaying the file location. Not sure if “date” is the right name for the command that displays the current date? Try this:

[jdoe@fedora ~]$ which date
/usr/bin/date

3. cd

The cd (change directory) command will move you around the file system. An easy first step is to move into the /tmp directory and then back home by using the cd command without any arguments. Notice the change in the prompt (addition of “tmp”).

[jdoe@fedora ~]$ cd /tmp
[jdoe@fedora tmp]$ cd

4. pwd

The pwd (present working directory) command will confirm your current location in the file system.

[jdoe@fedora ~]$ pwd
/home/jdoe
[jdoe@fedora tmp]$ pwd
/tmp

5. mkdir

The mkdir (pronounced “make dir”) command, as the name suggests, creates a new directory. Obviously, this depends on whether you have write permission to the file’s intended location. If you don’t supply a full path for the new directory, it will be set up in your current file system location.

[jdoe@fedora ~]$ mkdir reports
[jdoe@fedora ~]$ ls -ld reports
drwxr-xr-x. 1 jdoe jdoe 0 Apr 3 14:39 reports

6. rm

The rm command will remove files or directories. You will need write permission to remove a file or you will see an error like this one:

[jdoe@fedora tmp]$ rm /usr/bin/date
rm: remove write-protected regular file '/usr/bin/date'? y
rm: cannot remove '/usr/bin/date': Permission denied

To remove a directory, you can use a recursive rm command (e.g., rm -r dirname) or a rmdir command if the directory is empty.

7. cp

The cp command will make a copy of a file. The command shown below makes a backup of a file that you might be planning to modify.

[jdoe@fedora ~]$ cp notes notes.save
[jdoe@fedora ~]$ ls
notes notes.save reports

8. mv

The mv (mov) command till move a file to a different location in the file system or change its name. The first mv command below just renames a file. The second moves a file to a different location.

[jdoe@fedora ~]$ mv myfile oldfile
[jdoe@fedora ~]$ ls
notes notes.save oldfile reports
[jdoe@fedora ~]$ mv oldfile /tmp
[jdoe@fedora ~]$ ls
notes notes.save reports

9. touch

The touch command will create an empty file or, if that file already exists, will update the date/time stamps on the file.

[jdoe@fedora ~]$ touch newfile
[jdoe@fedora ~]$ ls -l newfile
-rw-r--r--. 1 jdoe jdoe 0 Apr 3 15:27 newfile

10. cat

The cat command will display the content of a text file.

[jdoe@fedora ~]$ cat notes
Staff meeting every Friday @ 1PM

The more, head and tail commands can also display the content of text files – a page at a time or the first or last lines as shown in these examples:

11. more

The more command will display the content of a text file a screenful at a time if the file has more lines than the screen can display.

[jdoe@fedora ~]$ more notes

12. head

The head command will display the first lines of a text file. You can display only the top three lines of a text file with a command like this:

[jdoe@fedora ~]$ head -3 /etc/passwd
root:x:0:0:Super User:/root:/bin/bash
bin:x:1:1:bin:/bin:/usr/sbin/nologin
daemon:x:2:2:daemon:/sbin:/usr/sbin/nologin

13. tail

The tail command is the opposite of the head command. You can display just the last three lines of a file with a command like this:

[jdoe@fedora ~]$ tail -3 /etc/passwd
fedora:x:1000:1000:fedora:/home/fedora:/bin/bash
shs:x:1001:1001::/home/shs:/bin/bash
jdoe:x:1002:1002::/home/jdoe:/bin/bash

14. vi, vim and nano

The vi, vim and nano commands allow you to edit text files. You will need to get used to whichever editor you select – how to add and modify their content. It’s a good idea to have a “cheat sheet” for your selected text editor available until you get comfortable with its commands. (See also: Using the vim editor in Linux to quickly encrypt and decrypt files)

15. man

The man command will display the man (manual) page for a particular command that will help you learn about how the command works and its available options. After viewing the first screenful of content, just press the Enter key to see the next screenful. The example below passes the output of the “man vi” command to the head command to display only the top ten lines.

[jdoe@fedora ~]$ man vi | head -10
VIM(1) General Commands Manual VIM(1)

NAME
vim - Vi IMproved, a programmer's text editor

SYNOPSIS
vim [options] [file ..]
vim [options] -
vim [options] -t tag
vim [options] -q [errorfile]

Take note of the | (called a “pipe”) character in the command above. It is used to pass the output of one command to another.

16. chmod

The chmod command will change the permissions on a file. You can, for example, make a file executable (e.g., if it’s a script) or ensure that only you can read it.

To make a file executable by anyone, you would use a command like  “chmod 755 myscript”. Before you use this command, you should understand that a command like chmod 755 will give read, write and execute permissions to you and read and execute permissions to other users.

17. ps

The ps command will list processes that are currently running on the system. Typing just “ps” will list processes that you are currently running. Using “ps -ef” or “ps -aux” will list all running processes.

[jdoe@fedora ~]$ ps
PID TTY TIME CMD
21534 pts/6 00:00:00 bash
29486 pts/6 00:00:00 ps

This command displays a count of the currently running processes by passing the output of the ps command to the wc -l command which counts the lines:

[jdoe@fedora ~]$ ps -ef | wc -l
265

18. grep

The grep command will find instances of a specified string in text files. You need to provide a string and the name of the file you want the command to search. Run a command like that shown below and it will display the data that describes your home directory. Note that the ^ specifies the beginning of the line and $USER will be your username.

[jdoe@fedora ~]$ echo $USER
jdoe
[jdoe@fedora ~]$ grep ^$USER /etc/passwd
jdoe:x:1002:1002::/home/jdoe:/bin/bash

The output above shows that jdoe’s userid and groupid are both equal to 1002 and that jdoe’s default shell is /bin/bash. This information is derived from the /etc/passwd file.

Wrap-up

Running commands on the Linux command line can be a bit challenging at first but, as you get used to it, you’ll be surprised how much work you can get done with relatively little effort.

Source:: Network World