Linux is a Unix-Like operating system. All the Linux/Unix commands are run in the terminal provided by the Linux system. This terminal is just like the command prompt of Windows OS. Linux/Unix commands are case-sensitive. The terminal can be used to accomplish all Administrative tasks. This includes package installation, file manipulation, and user management. Linux terminal is user-interactive. The terminal outputs the results of commands which are specified by the user itself. Execution of typed command is done only after you press the Enter key.
Linux includes a large number of commands, but we’ve chosen some of the most important ones to present here.
The alias command lets you give your own name to a command or sequence of commands. You can then type your short name, and the shell will execute the command or sequence of commands for you.
This sets up an alias called
cls . It will be another name for
clear . When you type
cls, it will clear the screen just as though you had typed
clear . Your alias saves a few keystrokes, sure. But, if you frequently move between Windows and Linux command line, you can find yourself typing the Windows
cls command on a Linux machine that doesn’t know what you mean. Now it will know.
Aliases can be much more intricate than that simple example. Here’s an alias called
pf (for process find) that is just a little more complex. Note the use of quotation marks around the command sequence. This is required if the command sequence has spaces in it. This alias uses the
ps command to list the running processes and then pipes them through the
grep command. The
grep command looks for entries in the output from
ps that match the command line parameter
alias pf="ps -e | grep $1"
cat command (short for “concatenate”) lists the contents of files to the terminal window. This is faster than opening the file in an editor, and there’s no chance you can accidentally alter the file. To read the contents of your
.bash_log_out file, type the following command while the home directory is your current working directory, as it is by default
cd command changes your current directory. In other words, it moves you to a new place in the filesystem.
If you are changing to a directory that is within your current directory, you can simply type
cd and the name of the other directory.
If you are changing to a directory elsewhere within the filesystem directory tree, provide the path to the directory with a leading /.
To quickly return to your home directory, use the
~ (tilde) character as the directory name.
You can use the double dot symbol
.. to represent the parent of the current directory. You can type the following command to go up a directory.
Imagine you are in a directory. The parent directory has other directories in it, as well as the directory you’re currently in. To change into one of those other directories, you can use the
.. symbol to shorten what you have to type.
chmod command sets the file permissions flags on a file or folder. The flags define who can read, write to or execute the file. When you list files with the
-l (long format) option you’ll see a string of characters that look like
If the first character is a
- the item is a file, if it is a
d the item is a directory. The rest of the string is three sets of three characters. From the left, the first three represent the file permissions of the owner, the middle three represent the file permissions of the group and the rightmost three characters represent the permissions for others. In each set, an
r stands for read, a
w stands for write, and an
x stands for execute.
x character is present that file permission is granted. If the letter is not present and a
- appears instead, that file permission is not granted.
One way to use
chmod is to provide the permissions you wish to give to the owner, group, and others as a 3 digit number. The leftmost digit represents the owner. The middle digit represents the group. The rightmost digit represents the others. The digits 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, write and execute permissions
Looking at our example.txt file, we can see that all three sets of characters are
rwx. That means everyone has read, write and execute rights with the file.
To set the permission to be read, write, and execute (7 from our list) for the owner; read and write (6 from our list) for the group; and read and execute (5 from our list) for the others we’d need to use the digits 765 with the
chmod -R 765 example.txt
To set the permission to be read, write and execute (7 from our list) for the owner, and read and write (6 from our list) for the group and for the others we’d need to use the digits 766 with the
chmod 766 example.txt
chown command allows you to change the owner and group owner of a file. Listing our example.txt file with
ls -l we can see
dave dave in the file description. The first of these indicates the name of the file owner, which in this case is the user
dave. The second entry shows that the name of the group owner is also
dave. Each user has a default group created when the user is created. That user is the only member of that group. This shows that the file is not shared with any other groups of users.
You can use
chown to change the owner or group, or both of a file. You must provide the name of the owner and the group, separated by a
: character. You will need to use
sudo. To retain dave as the owner of the file but to set mary as the group owner, use this command:
sudo chown dave:mary example.txt
To change both the owner and the group owner to mary, you would use the following command:
sudo chown mary:mary example.txt
To change the file so that dave is once more the file owner and the group owner, use this command:
sudo chown dave:dave example.txt
curl command is a tool to retrieve information and files from Uniform Resource Locators (URLs) or internet addresses.
curl command may not be provided as a standard part of your Linux distribution. Use
apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.
sudo apt-get install curl
Suppose you want to retrieve a single file from a GitHub repository. There is no officially supported way to this. You’re forced to clone the entire repository. With
curl however, we can retrieve the file we want on its own.
This command retrieves the file for us. Note that you need to specify the name of the file to save it in, using the
-o (output) option. If you do not do this, the contents of the file are scrolled rapidly in the terminal window but not saved to your computer.
curl https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
If you don’t want to see the download progress information use the
-s (silent) option.
curl -s https://raw.githubusercontent.com/torvalds/linux/master/kernel/events/core.c -o core.c
df command shows the size, used space, and available space on the mounted filesystems of your computer.
Two of the most useful options are the
-h (human readable) and
-x (exclude) options. The human-readable option displays the sizes in Mb or Gb instead of in bytes. The exclude option allows you to tell
df to discount filesystems you are not interested in. For example, the
squashfs pseudo-filesystems that are created when you install an application with the
df -h -x squashfs
diff command compares two text files and shows the differences between them. There are many options to tailor the display to your requirements.
-y (side by side) option shows the line differences side by side. The
-w (width) option lets you specify the maximum line width to use to avoid wraparound lines. The two files are called text1.txt and text2.txt in this example. The
diff from listing the matching lines, letting you focus on the lines which have differences.
diff -y -W 70 text1.txt text2.txt --suppress-common-lines
echo command prints (echoes) a string of text to the terminal window.
The command below will print the words “A string of text” on the terminal window.
echo A string of text
echo command can show the value of environment variables, for example, the
$PATH environment variables. These hold the values of the name of the user, the user’s home directory, and the path searched for matching commands when the user types something on the command line.
echo $USER echo $HOME echo $PATH
The exit command will close a terminal window, end the execution of a shell script, or log you out of an SSH remote access session.
find command to track down files that you know exist if you can’t remember where you put them. You must tell
find where to start searching from and what it is looking for. In this example, the
. matches the current folder and the
-name option tells
find to look for files with a name that matches the search pattern.
You can use wildcards, where
* represents any sequence of characters and
? represents any single character. We’re using
*ones* to match any file name containing the sequence “ones.” This would match words like bones, stones, and lonesome.
find . -name *ones*
We can tell
find to restrict the search to files only. We do this using the
-type option with the
f parameter. The
f parameter stands for files.
find . -type f -name *ones*
If you want the search to be case insensitive use the
-iname (insensitive name) option.
find . -iname *wild*
finger command gives you a short dump of information about a user, including the time of the user’s last login, the user’s home directory, and the user account’s full name.
free command gives you a summary of the memory usage with your computer. It does this for both the main Random Access Memory (RAM) and swap memory. The
-h (human) option is used to provide human-friendly numbers and units. Without this option, the figures are presented in bytes.
grep utility searches for lines which contain a search pattern. When we looked at the alias command, we used
grep to search through the output of another program,
ps . The
grep command can also search the contents of files. Here we’re searching for the word “train” in all text files in the current directory.
grep train *.txt
The output lists the name of the file and shows the lines that match. The matching text is highlighted.
groups command tells you which groups a user is a member of.
gzip command compresses files. By default, it removes the original file and leaves you with the compressed version. To retain both the original and the compressed version, use the
-k (keep) option.
gzip -k core.c
head command gives you a listing of the first 10 lines of a file. If you want to see fewer or more lines, use the
-n (number) option. In this example, we use
head with its default of 10 lines. We then repeat the command asking for only five lines.
head -n 5 core.c
The history command lists the commands you have previously issued on the command line. You can repeat any of the commands from your history by typing an exclamation point
! and the number of the command from the history list.
Typing two exclamation points repeats your previous command.
kill command allows you to terminate a process from the command line. You do this by providing the process ID (PID) of the process to
kill. Don’t kill processes willy-nilly. You need to have a good reason to do so. In this example, we’ll pretend the
shutter program has locked up.
To find the PID of
shutter we’ll use our
grep trick from the section about the
alias command, above. We can search for the
shutter process and obtain its PID as follows:
ps -e | grep shutter.
Once we have determined the PID—1692 in this case—we can kill it as follows:
This might be the first command the majority of Linux users meet. It lists the files and folders in the directory you specify. By default,
ls looks in the current directory. There are a great many options you can use with
ls, Some common examples are presented here.
To list the files and folders in the current directory:
To list the files and folders in the current directory with a detailed listing use the
-l (long) option:
To use human-friendly file sizes include the
-h (human) option:
To include hidden files use the
-a (all files) option:
The man command displays the “man pages” for a command in
less . The man pages are the user manual for that command. Because
less to display the man pages, you can use the search capabilities of
mkdir command allows you to create new directories in the filesystem. You must provide the name of the new directory to
mkdir. If the new directory is not going to be within the current directory, you must provide the path to the new directory.
To create two new directories in the current directory use
To create a new directory called “2019” inside the “invoices” directory, use this command:
If you are going to create a directory, but its parent directory does not exist, you can use the
-p (parents) option to have
mkdir create all of the required parent directories too. In the following command, we are creating the “2019” directory inside the “yearly” directory inside the “quotes” directory. The “yearly” directory does not exist, but we can have
mkdir create all the specified directories at once:
mkdir -p quotes/yearly/2019
mv command allows you to move files and directories from directory to directory. It also allows you to rename files.
To move a file you must tell
mv where the file is and where you want it to be moved to. In this example, we’re moving a file called
apache.pdf from the “~/Document/Ukulele” directory and placing it in the current directory, represented by the single
mv ~/Documents/Ukulele/Apache.pdf .
To rename the file, you “move” it into a new file with the new name.
mv Apache.pdf The_Shadows_Apache.pdf
The file move and rename action could have been achieved in one step:
mv ~/Documents/Ukulele/Apache.pdf ./The_Shadows_Apache.pdf
passwd command lets you change the password for a user. Just type
passwd to change your own password.
You can also change the password of another user account, but you must use
sudo. You will be asked to enter the new password twice.
sudo passwd user1
ping command lets you verify that you have network connectivity with another network device. It is commonly used to help troubleshoot networking issues. To use
ping, provide the IP address or machine name of the other device.
ping command will run until you stop it with Ctrl+C.
ping to run for a specific number of ping attempts, use the
-c (count) option.
ping -c 5 <code>IP/Website</code>
To hear a ping, use the
-a (audible) option.
ping -a <code>IP/Website</code>
ps command lists running processes. Using
ps without any options causes it to list the processes running in the current shell.
To see all the processes related to a particular user, use the
-u (user) option. This is likely to be a long list, so for convenience pipe it through
ps -u user1 | less
To see every process that is running, use the
-e (every process) option:
ps -e | less
pwd command prints the working directory (the current directory) from the root / directory.
The shutdown command lets you shut down or reboot your Linux system.
shutdown with no parameters will shut down your computer in one minute.
To shut down immediately, use the
Use the ssh command to make a connection to a remote Linux computer and log into your account. To make a connection, you must provide your user name and the IP address or domain name of the remote computer. In this example, the user mary is logging into the computer at 192.168.4.23. Once the connection is established, she is asked for her password.
sudo command is required when performing actions that require root or superuser permissions, such as changing the password for another user.
sudo passwd user1
tar command, you can create an archive file (also called a tarball) that can contain many other files. This makes it much more convenient to distribute a collection of files. You can also use
tar to extract the files from an archive file. It is common to ask
tar to compress the archive. If you do not ask for compression, the archive file is created uncompressed.
To create an archive file, you need to tell
tar which files to include in the archive file, and the name you wish the archive file to have.
In this example, the user is going to archive all of the files in the project directory, which is in the current directory.
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 project/
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 project/
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 project/
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.
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.
tar -xvf songs.tar
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
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.