0% found this document useful (0 votes)
120 views19 pages

Basic Linux Commands For Linux Terminal Beginners - PCsteps

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.

com

Home Software Operating Systems Linux Basic Linux Commands for Linu

Search

Basic Linux Commands


for Linux Terminal
Beginners
May 26, 2017
Author: Angelos Kyritsis
41,000 2

(10 votes, average: 5.00 out of 5)

68
Facebook Twitter

Print More
SHARES

If you run a Linux distribution, and you are only using the
GUI, you are missing out. The Linux terminal is an
extremely powerful tool that goes well beyond the GUI.
Writing commands might seem scary for a beginner, but
https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 1/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

you will soon get the hang of it. In this guide, you will find
the most important Linux commands, to use the terminal
like a pro.

We have prepared this guide on Linux Mint 18.1 and


Ubuntu 16.04 LTS.

S e a r cThe
h basic Linux commands, however, are universal, and
they should work on any past and future distribution.
They will even work with non-Linux operating systems
based on Unix, such as FreeBSD, or the macOS / OS X
terminal.

Table of Contents [show]

Important information
about the Linux terminal
There is not a single Linux distribution, as far as we know,
that doesn't have a terminal of some kind. On the
contrary, there are a few distributions that don't have a
GUI by default, and everything is done on the command
line.

To open a terminal quickly from the GUI, the shortcut


Ctrl+Alt+T will work on most distributions and desktop
https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 2/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

environments.

This is the basic anatomy of most Linux commands:

[sudo] command [optional switch] [file or


directory path]

Search
Using sudo will run any command with administrative
rights. Most Linux commands that have to deal with
system files and installation/uninstallation of programs
demand sudo.

The linux commands


are case-sensitive
It is important to remember that everything written in the
terminal is case-sensitive. When the command is "sudo",
neither "Sudo", "SUDO", nor "sUdO" will work.

Most Linux commands are lowercase, but there are


capitalized switches, such as "chown -R".

The file and directory names are also case-sensitive.


"File1" and "file1" are different files, even if they are in the
same directory.

Beware of spaces
Spacing is equally important. "chown-R" will only return
an error. If we want to create/access/delete a file or

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 3/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

directory that has a space in the filename, we can either


put the whole filename inside quotation marks...

S e a r c...or
h "escape" the space using the backslash "\".

If we did neither, the particular mkdir command, which


creates directories, would create two directories, "Folder"
and "Name". Other Linux commands would just fail.

Finding previous Linux


commands
Pressing the Up keyboard key will cycle through the last
Linux commands we successfully used, in order. No
failed commands will show here.

We can also use the history command to see all the


Linux commands we have ever used on the terminal.

The invisible password


https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 4/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

When we are asked for our password, e.g. after we used


"sudo", as we type the password nothing will show on
screen, no stars or dots or anything. We just type the
password and press Enter.

Search

If you want to change the default behavior and have stars


appear while typing your password, check out our guide:

Show Stars when Typing the Linux


Password at the Terminal
(4
votes, average:
5.00 out of 5)
When we
enterour Linux
password at the
terminal,
nothing will
show as we are

Use Tab to autocomplete


The Tab button on the keyboard is a huge time saver on
Linux commands, as it will automatically fill in the names
of files and directories.

If we want to delete a file named


"whydidIgivethisfilesuchalongname", we just need to type
"rm w" and pressing Tab will automatically complete the
rest of the filename.

If there are more than files that begin with the same
letters, e.g. "whydidIgivethisfilesuchalongname" and

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 5/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

"whydidIeatsomuch", pressing Tab on "rm w" will


autocomplete the common "whydidI".

Search

We then need to type an additional character - "g" or "e",


in the example - and press again Tab for the
autocompletion to resume.

Copying and pasting Linux


commands
To copy or paste on the terminal, Ctrl+C and Ctrl+V won't
work.

Instead, we must use Ctrl+Shift+C and Ctrl+Shift+V. Or we


can right click and use the commands from the context
menu.

Wildcards
While using Linux commands, the characters "?" and "*"
are wildcards.

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 6/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

"?" will replace any single character. So, if we have two


files names test1file and test2file, we can delete them
both with "rm test?file". But this won't delete test12file.

Search
"*" will replace any string of characters. "rm test*file" will
delete test1file, test12file, testBLABLABLAfile. It will also
delete any other filename that begins with "test-" and
ends in "-file", including testfile.

It is obvious that we must be extremely careful when


combining deletion commands with wildcards. "rm *" will
delete every file in the current directory, and it won't use
Trash.

Extra information about


Linux commands
We can learn more about any of the Linux commands
with [command] --help and man [command].

[command] --help will show the usage of the command,


and the available options and switches.

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 7/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

Search
man [command] will show the command's manual, which
is an extended version of the --help output.

However, not all Linux commands have a manual or a --


help option.

Basic Linux commands


for the terminal
In this list, we haven't included every possible command,
just the Linux commands that would be more useful to a
Linux beginner. In the future, we will create a separate
guide with advanced Linux commands.

Basic navigation
https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 8/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

These Linux commands will help us navigate to particular


directories and search for files.
Full Posts Archive Social Media Windows Linux

Free Apps Awesome Sites Gaming


ls - list all the contents of Web Designworking directory
the current
Make sure you read ls --help because there are many
options on how to show the directory contents.

Search

cd [directory] - change directory

pwd - print the full path of the current working


directory. It stands for "Print Working Directory"

find [file or directory] - look for a particular file or


directory inside the current working directory
We can use it with wildcards, such as find *.png

locate [file or directory] - look for a specific file or


directory everywhere on the filesystem, and return all
paths that contain it.
We can use it with wildcards, such as locate *.png

Directory access shortcuts

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 9/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

When we want to navigate to a particular directory or


access a specific file, it's handy to keep in mind the
following shortcuts.

"~" represents our personal /home directory, e.g. cd ~


and cd ~/Documents

Search
".." represents the parent directory, i.e. the directory
that contains the one we currently are. If we are at
/home/test/public and type cd .. it will take us to
/home/test/

We can also use the full path to change to a particular


directory, e.g. cd /home/test/public/. The Tab button
can help us every step of the way to autocomplete the
directories.

What to do with commands that


return too many results
If we run "ls" on a directory with 1,000 files, or we use
"locate *.png" on a disk with lots of png pictures, we will
get too many results.

In this case, we can use a pipe with the vertical bar "|"
symbol (accessible with "Shift + \" ) and more or less.

With locate *.png | more we will get the results page by


page, and we can reveal the next pages by pressing
space. We quit by pressing "q".

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 10/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

Search

With locate *.png | less we will still get the first page of
results, but navigate up and down with the arrow keys.
Again, we quit with "q".

File and Directory Handling


This is how to create, delete, and search for files and
directories.

mkdir [directory name] - create a new directory


We can include the full path to the directory, to create
it anywhere on the disk, e.g. mkdir ~/public/dir1
If we create a directory with sudo mkdir, we won't be
able to create files or directories inside without
administrative rights.

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 11/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

touch [file name] - create an empty file


It is especially useful with the "echo" command, which
you will find in the next section of the guide.
If the file already exists, touch won't erase its contents,
it will just change the last access time to the current
time.
Search
cp [file1] [file2] - copy file1 and create a new file named
file2. The equivalent of Copy -> Paste.
To copy directories and their contents, we need to use
cp -r [dir1] [dir2].
We can include the full paths for both files or
directories to copy and paste from different parts of
the disk, e.g. cp ~/file1 ~/public/file2.

mv [/dir1/file1] [/dir2/file1] - move file1 from dir1 to dir2.


The equivalent of Cut -> Paste.
mv will work the same for directories, without needing
an extra switch.
The mv command is also the only way to rename a
file/directory using Linux commands. We just type mv
file1 file2 to rename file1 to file2.

rm [file] - delete a file


To delete a directory and all its contents, we need to
use rm -r [dir].
The rm command will immediately remove any
files/directories, without using Trash. So, be careful,
especially if you are using wildcards.
The sudo rm will remove system files and folders.

ln -s [file] [link] - create a symbolic link for a file or


folder
A symbolic link is a more powerful version of a

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 12/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

shortcut we create on Windows. There are some


differences that we will cover in a different guide.

zip [archive.zip] [file] - creates a zipped folder that


contains a compressed file.
Using wildcards, we can compress everything in a
directory with zip archive.zip *.
Search

unzip [archive.zip] - unzips a compressed folder.


By default, unzip will extract all the files in the working
directory.

tar -cf [archive.tar] [file] - creates a compressed folder


that contains a compressed file, using a different
algorithm than zip.
Using wildcards, we can compress everything in a
directory with tar -cf archive.tar *.

tar -xf [archive.tar] - decompresses a .tar archive.


By default tar -xf will extract all the files in the working
directory. With tar -zxf [archive.tar.gz] we can extract
tar.gz archives

gzip [file] - replaces the file with a .gz archive


containing the file.
Both zip and tar retain the original file but gzip will only
leave the .gz archive, and remove the original file.

gzip -d [archive.gz] - decompresses a .gz archive.


By default gzip -d will extract all the files in the working
directory.

unrar e [archive.rar] - decompresses a .rar archive.


Most distributions don't have rar preinstalled to create

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 13/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

a .rar archive, and can only install it as a trial version,


which needs a paid registration. We will find unrar
preinstalled on almost all distributions, however, to
decompress .rar archives.

Search

File content
Creating and deleting files and directories is fine. But
what if we want to access the file content at the Linux
Terminal?

echo "text" > [file.txt] - creates a file that includes


whatever "text" we wrote with the command
Echo just prints the "text" on the screen. Using the
redirection character ">" the result of echo is
redirected to the file.txt
Caution! If the file.txt already exists, the echo "text" >
file.txt will overwrite it, deleting all of its previous
content.

echo "text" >> [file.txt] - appends the "text" at the end


of the file.txt content
This is a safer way of running echo. If the file.txt file
exists, echo "text" >> file.txt will just add the "text" at
the bottom of the file's content, without overwriting the
previous content.
The > file.txt and >> file.txt parts of the command can
be used with all Linux commands that print a result

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 14/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

onscreen, such as ls, locate, etc. This will redirect or


append the result to a file.

cat [text file] - display all the contents of a text file at


the terminal.
For large files, we can use cat [text file] | more and cat
[text file] | less, the same way we pipe the output of ls
Search
or locate.
We can also use head [text file] to only show the first
ten lines, and tail [text file] to display the last ten lines.

grep "pattern" [file] - search for a particular text


pattern inside a text document. If the pattern is found,
the command prints the line containing the pattern on
the terminal.
We can also search for a pattern in all the files in a
directory, by using grep -r "pattern" [dir].

nano [text file] - nano is a relatively user-friendly


terminal text editor. We can also run nano with no
parameters, to create a new text file.
Just remember that the commands on the bottom all
work with Ctrl, e.g. we press Ctrl+X to exit, Ctrl+G to
get help, etc.

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 15/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

sort [text file] - will print the lines of text files sorted.

wc [text file] - will do a word count for a text file,


printing the numbers of newlines, words, and bytes, in
this order.

Search Are you looking for other


Linux commands?
If you want to know more about one of the Linux
commands we mentioned or are curious about another
Linux command, leave us a comment.

Did you enjoy this post?


Do you want to support PCsteps, so we can post high
quality articles each and every day?

You can like our Facebook page, share this post with
your friends, and select our affiliate links for your
purchases on Amazon.com or Newegg.

If you prefer your purchases from China, we are affiliated


with two of the largest international e-shops, GearBest
and Geekbuying.

68
Facebook Twitter

Print More
SHARES

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 16/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

Recommended for you


Search

Get a Free Photoshop Download Movies for How to Enable Port


Download Legally fr... Free with Torrent, Le... Forwarding on my R...

Run a Full macOS How To Create a All Valid Methods to


Sierra Mac OS X Virt... Software RAID 5 in L... Upgrade to Window...

Category: Linux

About Angelos Kyritsis


Angelos Kyritsis is the Editor-In-Chief of
PCsteps.gr, which is the #1 Computer
Hardware site in Greece.

He loves good beer, having more RAM on his PC that he


will ever, ever need, and writing for a living.

He has also published a novel, but it would be, quite


literally, all Greek to you.
https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 17/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

Download Movies for Windows Computer


Free with Torrent, Legally Maintenance with the Free
Glary Utilities Pro

Load Comments
Search

Custom Search

Search

Every Way to Download


Windows 7 ISOs, Legally and
for Free | PCsteps.com

Chrome Canary vs Chrome


and the Chrome Channels |
PCsteps.com

Predict if Hard Disk Failure is


Imminent with
CrystalDiskInfo |

Install Lubuntu 15.10: A Great


Windows XP Replacement |
PCsteps.com

How To Gain Administrator


Access in Windows without a
Password | PCsteps.com

How To Create a Windows


Installation Disc or Windows
USB | PCsteps.com

How To Change GRUB boot


order in Linux Mint / Ubuntu |
PCsteps.com

System Repair Disc: How to


Create a Rescue
CD/DVD/USB for Windows |

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 18/19
04/08/2017 Basic Linux Commands for Linux Terminal Beginners | PCsteps.com

Search

Copyright 20152017 PCsteps.com


About us
Didn't find what you were looking for? Use search to look for another of our guides:

Search

https://www.pcsteps.com/5010-basic-linux-commands-terminal/ 19/19

You might also like