Are you a Linux freshman or just a bit rusty? Here are all the commands you need to know. Think of this as an essential reference for the Linux terminal. This also applies to the macOS command line.
The Essential Toolkit for the Terminal
Linux contains a large number of commands, but we have selected 37 of the most important commands here. Learn these commands and you will be much more at home at the Linux prompt.
The following list is arranged alphabetically. The position of a command in the list is not representative of its usefulness or simplicity. The last word about using a command can be found in the man pages. The command
man is of course in our list ̵
. 1 Alias
Use the Alias command to give a command or command string its own name. You can then enter your nickname and the shell will run the command or command string for you.
alias cls = clear
This creates an alias named
cls . It will be another name for
clear . If you enter
cls the screen is cleared as if you had entered
as . Your alias stores a few keystrokes. However, if you frequently switch between Windows and Linux command lines, on a Linux machine, type cls cls which does not know what you mean. Now it will know.
Aliases can be much more complicated than this simple example. Here's an alias called
pf (for process finding), which is just a little bit more complex. Note the use of quotation marks around the command sequence. This is required if the command sequence contains spaces. This alias uses the command
ps to list the running processes, and then directs them through the command
grep . The command
grep looks for entries in the output of
ps that match the command line parameter
$ 1 . Alias pf = "ps -e | grep $ 1"
If you want to determine the process ID (PID) of the
process or if you want to find out if the
-shutter is executed at all, you can use the alias like this. Enter
pf a space and the name of the process you are interested in:
Aliase defined in the command line becomes die with the terminal window. If you close it, they are gone. To have your aliases always available, add them to
.bash_aliases in your home directory.
. 2 cat
cat (short for "concatenate") lists the contents of files in the terminal window. This is faster than opening the file in an editor, and you can not accidentally change the file. To read the contents of your file
.bash_log_out enter the following command while the home directory is your current working directory (by default):
For files that are longer than the number of lines in your terminal window, the text will pass too fast for you. You can whistle the output from
less to make the process more manageable. Press
less to scroll through the file using the up and down arrow keys, the PgUp and PgDn keys, and the Home and End keys. Enter
q to finish the number.
cat .bashrc | less
cd changes your current directory. In other words, you will be moved to a new location in the file system.
If you change to a directory that is in your current directory, you can simply type
cd and the name of the other directory
If you are in another directory within To change the directory structure of the file system, type the path to the directory with a leading /.[19659009dlcd/usr/local/bin
an to return quickly. In your home directory, use the character
~ (tilde ) as a directory name.
Here is another trick: You can use the double-dot
symbol. to represent the parent directory of the current directory. You can enter the following command to call a directory:
Imagine that you are in a directory. The parent directory contains other directories and the directory you are currently in. To switch to one of these other directories, you can use the
.. . Icon to shorten what you need to type.
chmodsets the file permission flags for a file or folder If you list files with the option
-l(long format), you will see a string that looks like-rwxrwxrwx
if the first character is a
-is] the element is a file, if it is a
dthe element is a directory, the rest of the string consists of three groups of three characters the first three represent the owner's file permissions  the middle three represent the file permissions of the group and the far right three characters the permissions for others . In each sentence is a
rfor reading, a
wis for writing, and a
xis for execute.  If the
xcharacter exists, the file is granted. If the letter does not exist and instead appears
-this file permission will not be granted.
One way to use
chmodis to grant the permissions that you want to give to the owner, group, and other than a 3-digit number. The number on the far left represents the owner. The middle digit stands for the group. The right digit stands for the others. The digits that you can use and what they represent are listed here:
- 0: No permission
- 1: execute permission
- 2: write permission
- 3: ] Write and Execute Permissions
- 4: Read Permission
- 5: Read and Execute Permissions
- 6: Read and Write Permissions
- 7: Read Executing Write and Permissions
If you look at our example.txt file, we can see that all three character sets are
rwx . That is, everyone has rights to the file with read, write and execute rights.
To set the permission to read, write and execute (7 from our list) for the owner Read and write (6 from our list) to the group and read and execute (5 from our list) for other we would need to use the digits 765 with the
chmod ] command:
chmod -R 765 example.txt
To set the permission to read, write and execute (7 from our list) for owner and read and write (6 from our list) for the group and for the others  we would have to use the digits 766 with
chmod 766 example.txt
chown command to change the owner and group owner of a file. If we list our example.txt file with
ls -l we can see
dave dave in the file description. The first of these specifies the name of the file owner. In this case, it is the user
dave . The second entry shows that the name of the group owner is also
dave . Each user has a default group that is created when the user is created. This user is the only member of this group. This shows that the file is not shared with other user groups.
You can use
chown to change the owner or group or both of a file. You must specify the name of the owner and the group, separated by a character
: . You must use
sudo . To keep dave as the owner of the file, but to set mary as the group owner, use this command:
sudo chown dave: mary example.txt
 For both the owner and Also to change the group owner in Mary, you would use the following command:
sudo chown mary: mary example.txt
To change the file so that dave is again the file owner and the group owner, use it Command:
sudo chown dave: dave example.txt
curl is a tool for retrieving information and files from URLs (Uniform Resource Locators) or Internet addresses.
curl may not be provided as a standard part of your Linux distribution. Use
apt-get to install this package on your system if you are using Ubuntu or any other Debian-based distribution. Instead, use the package management tool of your Linux distribution on other Linux distributions.
sudo apt-get install curl
For example, suppose you want to retrieve a single file from a GitHub repository. There is no officially supported way. You are forced to clone the entire repository. However, with
curl we can retrieve the desired file ourselves.
This command gets the file for us. Note that you must specify the name of the file in which you want to save the file. Use the option
-o (output). If you do not, the contents of the file in the terminal window will scroll quickly but will not be saved to your computer.
curl https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
If you do not see download progress information you want to use
curl -s https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c 
dfcommand displays size, used space, and available space in the attached file systems on your computer.
Two of the most useful options are
-x(exclude). The human-readable option displays the sizes in Mb or Gb instead of bytes. With the exclusion option, you can tell
dfwhich file systems you are interested in that you are not interested in. For example, the
squashfspseudo-file systems created when you install an application with
. Snapcommand.df -h -x squashfs
RELATED: Showing Free Space and Disk Usage From the Linux Terminal
diffcompares two text files and shows the differences between them. There are many options to customize the ad to your needs.
-y(side by side) displays the line differences side by side. Use the option
-w(width) to specify the maximum line width that should be used to avoid line breaks. The two files are called alpha1.txt and alpha2.txt in this example. The
difffrom listing the matching rows, so you can focus on the rows that have differences.diff -y -W70 alpha1.txt alpha2.txt --suppress common lines
RELATED: Compare by two text files in the Linux terminal
echoprints a text string into the terminal window.
The following command prints the words "one text string" in the terminal window.Echo A String
echocommand can display the value of environment variables, such as: For example,
$ HOMEand ] environment variables $ PATH . These include the values of the name of the user, the home directory of the user, and the path searched for matching commands when the user types something on the command line.echo $ USERecho $ HOMEecho $ PATH
The following command causes a beep to sound. The option
-e(escape code) interprets the escaped character as a bell.echo -e " a"
echois also invaluable in shell scripts. A script can use this command to generate a visible output to show the progress or results of the script as it executes.
The exit command closes a terminal window, stops the execution of a shell script, or logs you out of an SSH remote access session.exit
findcommand to find files that you know exist, if you can not remember where you put them. You need to find out
where to start searching and what to look for. In this example the
.matches the current folder and option
findto search for files with a name that matches the search pattern.
You can use wildcards where
*stands for any string and
for any single character. We use
* one *to find each filename that contains the sequence "one". This would fit words like bones, stones and lonesome.find. -name * ones *
As we can see,
findhas returned a list of matches. One of them is a directory called Ramones.
findcan limit the search to files. We do this with the option
-typewith the parameter
f. The parameter
fstands for files.find. -type f -name * ones *
If the search is case-sensitive, use the option
-iname(insensitive name).find. -iname * wild *
fingercommand provides a brief snapshot of information about a user, including the time the user last logged in, the user's home directory, and the user account's full name.
freegives you a summary of the memory usage of your computer. This is done for both random access memory (RAM) and swap memory. The option
-h(human) is used to indicate humane numbers and units. Without this option, the numbers are represented in bytes.free -h
greputility looks for lines that contain a search pattern. When we looked at the alias command, we used
grepto search the output of another program,
ps. The command
grepcan also search the contents of files. Here we search in all text files of the current directory for the word "train".grep train * .txt
The output lists the name of the file and displays the corresponding lines. The associated text is highlighted.
The functionality and sheer utility of
grepdefinitely guarantees checking out the man page.
groupstells you which groups a user is a member of.Groups daveGroups Mary
 16. gzip
gzipcompresses files. By default, the original file is removed and you receive the compressed version. To keep both the original and compressed versions, use the option
-k(keep).gzip -k core.c
 17. head
headgives you a list of the first 10 lines of a file. If you want to see fewer or more lines, use the option
-n(number). In this example we use
headwith a standard of 10 lines. We then repeat the command and only ask for five lines.head-core.chead -n 5 core.c
The history command lists the commands you previously issued on the command line. You can repeat every command from your history by making an exclamation mark
!and the number of the command from the history list! Enter 188
If you enter two exclamation marks, your previous command is repeated.!!
You can use the
killcommand to end a process from the command line. To do this, specify the process ID (PID) of the process
kill. Do not kill processes arbitrarily. You have to have a good reason for that. In this example, we specify that the program
for the trigger programhas been discontinued.
To determine the PID of the
switchwe use our
greptrick from the section on alias
above. We can search for the
Shutterprocess and get its PID like this:ps -e | grep shutter.
Once we have determined the PID - 1692 in this case - we can kill it as follows:kill 1692
You can use the
lesscommand to view files without opening an editor. The usage is faster and you do not have the option to change the file unintentionally. With
lessyou can use the up and down arrow keys, the PgUp and PgDn keys, and the Home and End keys to scroll through the file. Press the Q key to cancel .
To display a file, specify
lessas follows:less core.c 19659010]
You can also change the output of other commands to
forward less. Use the following command to view the output of
lsfor a listing of your entire hard disk:ls -R / | less
/to search forward in the file, and
to search backward.
This could be the first command encountered by the majority of Linux users. It lists the files and folders in the directory you specified. By default,
lssearches in the current directory. There are many options that you can use with
lsand we strongly recommend that you review the man page. Some common examples are presented here.
To list the files and folders in the current directory:ls
To list the files and folders in the current directory with a detailed list, use
-l(long) Option:  ls -l
To use friendly file sizes, use the option
files use the option
-a(all files):ls -lha
The command man displays the "man pages" for a command in
less. The manpages are the user manual for this command. Since
less to display the man pages, you can use the search functions of
For example, to use the man pages for [see
chownuse the following command:man chown
Use the up and down arrows or the PgUp and PgDn keys to navigate through the To scroll through the document. Press
qto exit the man page, or press
You can use the
mkdircommand to create new directories in the file system. You must specify
mkdirthe name of the new directory. If the new directory is not in the current directory, you must specify the path to the new directory.
To create two new directories in the current directory named "Invoices" and "Quotation marks", use the following two commands:mkdir invoicesmkdir quotes
To create a new directory named "2019" in the "Invoices" directory, use this command:  mkdir invoices / 2109
If you want to create a directory, the parent However, if the directory does not exist, you can use the option
-p(parent) to have
mkdir. Also, create all required parent directories. In the following command we create the directory "2019" in the directory "annual" in the directory "quotes". The directory & # 39; annual & # 39; is not present, but we can create
mkdirall specified directories simultaneously:mkdir -p quotes / annual / 2019
The "annual" directory is also created
You can use the
mvcommand to move files and directories from directory to directory. You can also rename files.
To move a file, you must tell
mvwhere the file is and where to move it. In this example we move a file named
apache.pdffrom the directory "~ / Document / Ukulele" and place it in the current directory, represented by the single
.characters.mv ~ / Documents / Ukulele / Apache.pdf.
To rename the file, "move" it to a new file with the new name.mv Apache.pdf The_Shadows_Apache.pdf
The action to move and rename files could be accomplished in one step:mv ~ / Documents / Ukulele / Apache.pdf ./The_Shadows_Apache.pdf __] 25. passwd
Use the command
passwdto change the password for a user. Just enter
passwdto change your own password.
You can also change the password of another user account, but you must use
sudo. You will be prompted to enter the new password twice.sudo passwd mary
You can use the
pingcommand to verify that you have a network connection with another network device. It is often used to troubleshoot network problems. To use
pingspecify the IP address or computer name of the other device.ping 192.168.4.18
pingruns until you stop it with Ctrl + C.
Here's What Happens:
- The device at IP address 192.168.4.18 responds to our ping requests and returns 64-byte packets
- Internet Control Messaging Protocol (ICMP) sequence numbering allows us to check for missed replies ( dropped packages).
- The TTL number is the "time to live" for a package. Each time the packet passes through a router, it is decremented by one. If it reaches zero, the package is thrown away. This is to prevent network loop problems from flooding the network.
- The time value is the duration of the roundtrip from your computer to the device and back. Put simply, the lower this time, the better.
ping to run for a certain number of ping attempts, use the option
-c (count).  ping -c 5 192.168.4.18
To hear a ping, use the option
ping -a 192.168.4.18
ps lists running processes. If
ps is used without options, the processes running in the current shell are listed.
To display all processes Use
-u (user) for a particular user. This is probably a long list, allowing for convenience pipe
ps -u dave | less
To see each running process, use the option
-e (all processes):
ps -e | less
pwd prints the working directory (the current directory) from the root directory / directory.
Use the shutdown command to shut down or restart your Linux system.
shutdown without parameters, your computer shuts down in a minute.
Zum sofortigen Herunterfahren verwenden Sie den Parameter
Sie können auch Planen Sie ein Herunterfahren und informieren Sie alle angemeldeten Benutzer über das bevorstehende Herunterfahren. Damit der Befehl
shutdown wissen kann, wann er heruntergefahren werden soll, geben Sie ihm eine Zeit. Dies kann eine festgelegte Anzahl von Minuten sein, wie
+90 oder eine genaue Zeit wie
23:00 . Jede von Ihnen bereitgestellte Kurzmitteilung wird an angemeldete Benutzer gesendet.
Herunterfahren 23:00 Uhr heute Nacht um 23:00 Uhr herunterfahren, Ihre Arbeit speichern und abmelden!
Verwenden Sie zum Abbrechen eines Herunterfahrens die Option
-c (Abbruch). Hier haben wir ein Herunterfahren für 15 Minuten geplant - und dann unsere Meinung geändert.
Herunterfahren +15 Herunterfahren in 15 Minuten!
RELATED: Neustarten oder Herunterfahren von Linux mithilfe der Befehlszeile
Verwenden Sie den Befehl ssh, um eine Verbindung zu einem Linux-Remotecomputer herzustellen und sich bei Ihrem Konto anzumelden. Um eine Verbindung herzustellen, müssen Sie Ihren Benutzernamen und die IP-Adresse oder den Domänennamen des Remote-Computers angeben. In diesem Beispiel meldet sich der Benutzer mary bei 192.168.4.23 am Computer an. Sobald die Verbindung hergestellt ist, wird sie nach ihrem Kennwort gefragt.
Ihr Benutzername und Ihr Kennwort werden überprüft und akzeptiert. und sie ist eingeloggt. Beachten Sie, dass sich ihre Eingabeaufforderung von "Nostromo" in "howtogeek" geändert hat.
Mary gibt den Befehl
w aus, um die aktuellen Benutzer im "howtogeek" -System aufzulisten. Sie wird als von pts / 1 verbunden aufgelistet, was ein Pseudo-Terminal-Slave ist. Das heißt, es ist kein Terminal, das direkt mit dem Computer verbunden ist.
Um die Sitzung zu beenden, verlässt Mary
sudo ist erforderlich, wenn Aktionen ausgeführt werden, für die Root- oder Superuser-Berechtigungen erforderlich sind, beispielsweise das Ändern des Kennworts für einen anderen Benutzer.
sudo passwd mary
 passwd in a Terminalfenster " width="644" height="145" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>
Mit dem Befehl
tail erhalten Sie eine Auflistung der letzten 10 Zeilen einer Datei. Wenn Sie weniger oder mehr Zeilen sehen möchten, verwenden Sie die Option
-n (Nummer). In diesem Beispiel verwenden wir
tail mit einer Standardeinstellung von 10 Zeilen. Wir wiederholen dann den Befehl und fordern nur fünf Zeilen auf.
tail -n 5 core.c
Mit dem Befehl
tar können Sie eine Archivdatei (auch Tarball genannt) erstellen, die viele andere Dateien enthalten kann. Dies macht es viel bequemer, eine Sammlung von Dateien zu verteilen. Sie können auch
tar verwenden, um die Dateien aus einer Archivdatei zu extrahieren. Es ist üblich,
tar zu bitten, das Archiv zu komprimieren. Wenn Sie keine Komprimierung anfordern, wird die Archivdatei unkomprimiert erstellt.
Zum Erstellen einer Archivdatei müssen Sie
tar angeben, welche Dateien in die Archivdatei aufgenommen werden sollen und welchen Namen Sie haben möchten Archivdatei zu haben.
In diesem Beispiel archiviert der Benutzer alle Dateien im Ukulele-Verzeichnis, das sich im aktuellen Verzeichnis befindet.
They have used the
-c (create) option and the
-v (verbose) option. The verbose option gives some visual feedback by listing the files to the terminal window as they are added to the archive. The
-f (filename) option is followed by the desired name of the archive. In this case, it is
tar -cvf songs.tar Ukulele/
The files are listed to the terminal window as they are added to the archive file.
There are two ways to tell
tar that you want the archive file to be compressed. The first is with the
-z (gzip) option. This tells tar to use the
gzip utility to compress the archive once it has been created.
It is usual to add “.gz” as suffix to this type of archive. That allows anyone who is extracting files from it to know which commands to pass to
tar to correctly retrieve the files.
tar -cvzf songs.tar.gz Ukulele/
The files are listed to the terminal window as they are added to the archive file as before, but the creation of the archive will take a little longer because of the time required for the compression.
To create an archive file that is compressed using a superior compression algorithm giving a smaller archive file use the
-j (bzip2) option.
tar -cvjf songs.tar.bz2 Ukulele/
Once again, the files are listed as the archive is created. The
-j option is noticeably slower than the
If you are archiving a great many files, you must choose between the
-z option for decent compression and reasonable speed, or the
-j option for better compression and slower speed.
As can be seen in the screenshot below, the “.tar” file is the largest, the “.tar.gz” is smaller, and the “.tar.bz2” is the smallest of the archives.
To extract files from an archive file use the
-x (extract) option. The
-v (verbose) and
-f (filename) options behave as they do when creating archives. Use
ls to confirm which type of archive you are going to extract the files from, then issue the following command.
tar -xvf songs.tar
The files are listed as they are extracted. Note that the Ukulele directory is also recreated for you.
To extract files from a “.tar.gz” archive, use the
-z (gzip) option.
tar -xvzf songs.tar.gz
Finally, to extract files from a “.tar.bz2” archive use the
-j option instead of the
-z (gzip) option.
tar -xvjf songs.tar.bz2
RELATED: How to Extract Files From a .tar.gz or .tar.bz2 File on Linux
top command shows you a real-time display of the data relating to your Linux machine. The top of the screen is a status summary.
The first line shows you the time and how long your computer has been running for, how many users are logged into it, and what the load average has been over the past one, five, and fifteen minutes.
The second line shows the number of tasks and their states: running, stopped, sleeping and zombie.
The third line shows CPU information. Here’s what the fields mean:
- us: value is the CPU time the CPU spends executing processes for users, in “user space”
- sy: value is the CPU time spent on running system “kernel space” processes
- ni: value is the CPU time spent on executing processes with a manually set nice value
- id: is the amount of CPU idle time
- wa: value is the time the CPU spends waiting for I/O to complete
- hi: The CPU time spent servicing hardware interrupts
- si: The CPU time spent servicing software interrupts
- st: The CPU time lost due to running virtual machines (“steal time”)
The fourth line shows the total amount of physical memory, and how much is free, used and buffered or cached.
The fifth line shows the total amount of swap memory, and how much is free, used and available (taking into account memory that is expected to be recoverable from caches).
The user has pressed the E key to change the display into more humanly digestible figures instead of long integers representing bytes.
The columns in the main display are made up of:
- PID: Process ID
- USER: Name of the owner of the process
- PR: Process priority
- NI: The nice value of the process
- VIRT: Virtual memory used by the process
- RES: Resident memory used by the process
- SHR: Shared memory used by the process
- S: Status of the process. See the list below of the values this field can take
- %CPU: the share of CPU time used by the process since last update
- %MEM: share of physical memory used
- TIME+: total CPU time used by the task in hundredths of a second
- COMMAND: command name or command line (name + options)
(The command column didn’t fit into the screenshot.)
The status of the process can be one of:
- D: Uninterruptible sleep
- R: Running
- S: Sleeping
- T: Traced (stopped)
- Z: Zombie
Press the Q key to exit from
RELATED: How to Set Process Priorities With nice and renice on Linux
You can obtain some system information regarding the Linux computer you’re working on with the
- Use the
-a(all) option to see everything.
- Use the
-s(kernel name) option to see the type of kernel.
- Use the
-r(kernel release) option to see the kernel release.
- Use the
-v(kernel version) option to see the kernel version.
w command lists the currently logged in users.
whoami to find out who you are logged in as or who is logged into an unmanned Linux terminal.
RELATED: How to Determine the Current User Account in Linux
That’s Your Toolkit
Learning Linux is like learning anything else. You’re going to need some practice before your familiar with these commands. Once you have these commands at your fingertip, you’ll be well along the path to proficiency.
There’s an old joke—probably as old as Unix itself—that says the only command you need to know is the
man command. There’s a glimmer of truth in that, but some of the man pages are impenetrable without an introduction. This tutorial should give you the introduction you need.