Terminal Commands
This post will cover some of the more popular and useful terminal commands to know when you’re working through an SSH connection/session and only have access to the CLI. (Command Line Interface)
Overview
ls |
cd |
mkdir |
touch |
Show directory contents (List information of files) | Change current/working directory | Make a directory (Create a new folder) | Change file timestamps or create a file if specified file does not exist |
rm |
cat |
pwd |
cp |
Remove a file or directory | Print contents of a file or concenates two files into one new file | Print working directory (Shows the full path to where you are right now in a terminal session) | Copy a file/folder |
mv |
grep |
find |
Vi - Text Editor |
Move (or rename) a file/folder | Search for a specific phrase in file/lines | Search files and directories | One of the most popular text editors (Old-school, generally considered difficult to learn for a beginner) |
Emacs - Text Editor | Nano - Text Editor | history |
clear |
Also one of the most popular text editors (Easy for beginners) | The last of the three most popular text editors worldwide (Also fairly easy for beginners) | Show last used commands | Clear the terminal screen |
tar |
wget |
du |
|
Create or Unpack compressed archives (tar or untar an archive) | Download files from the internet | Get file sizes and disk usage (du = disk usage) | |
ls
- list directory contents
List information about the files (the current directory by default). After entering ls, you will see an output that looks like this:
user@hostname:/var/www $ ls
html ncp-app ncp-previewgenerator ncp-web nextcloud
user@hostname:/var/www $ ls -a
. .. html ncp-app ncp-previewgenerator ncp-web nextcloud
user@hostname:/var/www $ ls -l
total 20
drwxr-xr-x 2 root root 4096 Oct 8 21:31 html
drwxr-xr-x 8 root root 4096 Oct 28 23:08 ncp-app
drwxr-xr-x 4 www-data www-data 4096 Oct 28 23:08 ncp-previewgenerator
drwxrwx--- 8 www-data www-data 4096 Oct 15 13:18 ncp-web
drwxr-x--- 14 www-data www-data 4096 Oct 14 11:05 nextcloud
user@hostname:/var/www $ ls -la
total 28
drwxr-xr-x 7 root root 4096 Oct 28 23:08 .
drwxr-xr-x 12 root root 4096 Oct 12 15:38 ..
drwxr-xr-x 2 root root 4096 Oct 8 21:31 html
drwxr-xr-x 8 root root 4096 Oct 28 23:08 ncp-app
drwxr-xr-x 4 www-data www-data 4096 Oct 28 23:08 ncp-previewgenerator
drwxrwx--- 8 www-data www-data 4096 Oct 15 13:18 ncp-web
drwxr-x--- 14 www-data www-data 4096 Oct 14 11:05 nextcloud
Commonly used options
-l — displays the details of the files, such as size, modified date and time, the owner, and the permissions.
-a — shows hidden files and directories.
Manual pages
man ls
cd
- change directory
cd
is the command used to change your current working directory to another in a terminal session. It’s a pretty simple command — just type cd
followed by the name or PATH
to the directory:
cd /directory
or
cd /path/to/directory
As such, if you want to enter the home directory of your server, you can type:
cd $home
or
cd /home/username/Some_directory/And_another_one
You can see that you’ve moved your working directory of the terminal session to the new directory as it becomes appended to username@hostname:~ $
in your console, like so:
username@hostname:/var/www $
To move back up one directory simply put ..
(two dots) after the cd
command.
What’s cool here, is you can go back further by adding another two-dots and separating them with a forward slash (/), so if you want to return several levels up in the directory.
Like so:
cd ../..
Manual pages
man cd
mkdir
- make directories
The mkdir
(Make directory) command is used to create a directory, if the do not already exist.
The command below will create a directory in the current working directory of the terminal session.
mkdir folder_name
Let’s assume you want to create a new folder named “myfolder” in the current working directory of the terminal. All you need to do is enter:
mkdir myfolder
And it will create the directory, unless it already exists. You can also specify a PATH
prepending the name of the folder if you wish to create a directory outside the current working directory of the terminal.
mkdir $HOME/path_to/create_a/new_directory
mkdir /home/username/path_to/create_a/new_directory
Manual pages
man mkdir
touch
- change file timestamps
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h is supplied.
A FILE argument string of “-
” is handled specially and causes touch to change the times of the file associated with standard output.
touch file_name
If you want to create a .txt file named “myfile” in the current working directory
touch myfile.txt
Manual pages
man touch
rm
- remove files or directories
rm
removes each specified file. By default, it does not remove directories.
To delete a file in your current working directory, enter:
rm file_name
For instance, if you want to remove myfile.txt
, provided the file is in your current working directory, execute:
rm myfile.txt
To delete a directory (folder) and all of it’s content and sub-directories, you need to use the command with the -r
(recursive) option to remove all the files and subfolders inside it
rm -r home/username/path_to/myfolder
Manual pages
man rm
cat
- concatenate files and print on the standard output
The cat
command is used to display the content of a file.
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
cat
file_name
or
cat
/path/to/file_name
It also allows you to create a new file by merging multiple files line output.
For example:
cat info.txt info2.txt > mergedinfo.text
By executing this line, the content of info.txt and info2.txt will be saved into mergedinfo.txt.
Manual pages
man cat
pwd
- print name of current/working directory
pwd is a simple command that outputs the full path of your working directory.
Once entered, assuming you are currently in a directory (folder) located in the base your $HOME
directory, you should see a result like this:
home/username/folder
pwd command can come in really handy when you are accessing your shared hosting account through SSH. Oftentimes, shared servers don’t tell you the directory you are in.
Manual pages
man pwd
cp
- copy files and directories
This command is used for coping files or folders.
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
Synopsis
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
SOURCE
is the source file or folder you want to copy and DESTINATION
is well, kind of self-explanatory.
Let’s say you have myfile.txt
in your working directory, and you want to make a copy of it in the same directory
cp myfile.txt myfile2.txt
If you want a copy in a different directory
cp /home/username/myfile.txt /home/path_to/directory_of/file_copy
Be careful when writing the name of the destination.
If you provide two file names, the cp
command will copy the content of the source file into the destination file. Thus, the destination file will be overwritten without any warning.
If the destination file doesn’t exist, then the command will create a new file.
Commonly used options
-f, --force
if an existing destination file cannot be opened, remove it and try again (this option is ignored when
the -n option is also used)
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is miss‐
ing
-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)
-a, --archive
same as -dR --preserve=all
Unlike copying a file, copying directories requires you to use the -R (recursive) option.
This option recursively selects and copies all files and sub-directories inside the specified SOURCE
directory to the specified DESTINATION
directory.
cp -R /home/username/myfolder /home/username/path_to/destination
Manual pages
man cp
mv
- move (or rename) files
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
This command works similarly to cp
.
However, mv
command will move the SOURCE
file or directory instead of copying it.
SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
Let’s say we want to move myfile.txt
from /home/username/ftp
to /home/username/myfolder/
mv /home/username/ftp/myfile.txt /home/username/myfolder
Unlike the cp
command, you don’t need the -R
option to move a directory.
mv /home/hostinger/ftp/ /home/hostinger/myfolder/
The SOURCE
can be one, or more files or directories, and DESTINATION
can be a single file or directory.
-
When multiple files or directories are given as a
SOURCE
, theDESTINATION
must be a directory. In this case, theSOURCE
files are moved to the target directory. -
If you specify a single file as
SOURCE
, and theDESTINATION
target is an existing directory, then the file is moved to the specified directory. -
If you specify a single file as
SOURCE
, and a single file asDESTINATION
target then you’re renaming the file. -
When the
SOURCE
is a directory andDESTINATION
doesn’t exist,SOURCE
will be renamed toDESTINATION
. Otherwise ifDESTINATION
exist, it be moved inside theDESTINATION
directory.
To move a file or directory, you need to have write permissions on both SOURCE
and DESTINATION
. Otherwise, you will receive a permission denied error.
Manual pages
man mv
grep
- print lines that match patterns
DESCRIPTION
grep searches for PATTERNS in each FILE. PATTERNS is one or patterns separated by newline characters, and grep prints each line that matches a pattern.A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.
In addition, the variant programs
egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.
These variants are deprecated, but are provided for backward compatibility.
grep 'line' info.txt
The above command would search for 'line'
in a file named info.txt
.
What’s great about this, is that the command will print the entire line that contains the matched text.
Keep in mind that this command is case sensitive, if you want to ignore case distinctions, use the -i
option.
-i, --ignore-case
Ignore case distinctions, so that characters that differ only in case match each other.
Manual pages
man grep
find
- search for files in a directory hierarchy
Description in manual
GNU
find
searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.
If no starting-point is specified, `.’ is assumed.If you are using
find
in an environment where security is important (for example if you are using it to search directories that are writable by other users), you should read theSecurity Considerations
chapter of thefindutils
documentation, which is calledFinding Files
and comes withfindutils
.
We use this command to search for a file or files that meet the given criteria (name, size, file type, etc).
find [starting directory] [options] [search term]
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
[starting directory]
is where you would like to start your search process from.
There are three main choices:
/ (slash) — search the whole system
. (dot) — search the working directory
~ (tilde) — search the home directory
[options] is additional argument(s) that you can use to refine your search.
Some of the more popular options are:
-name — look for files based on their names
-user — search for files that belong to a given user
-size — look for files based on their sizes
[search term]
is the keyword or number that you use to search for files.
find . -name “index”
This command will return any files that have the word “index” in their names. And since we use “.
” (dot), the command will only search the working directory.
Manual pages
man find
Vi, Nano and Emacs Text Editors
vi
(or vim which is vi improved) and nano
together with emacs
are the three most popular text editors worldwide that you can use in the command line used by coders all over the globe.
Open a file with Vi
vi file_name
Open a file with Nano
nano file_name
Open a file with Emacs
emacs file_name
Manual pages
man vi
man nano
man emacs
history
DESCRIPTION
Many programs read input from the user a line at a time. The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones.
This one is used to display the last used commands. You need to enter a number to limit the displayed results. For example:
history 20
As you probably guess, the example will show the 20 most recently entered terminal commands.
Manual pages
man history
clear
The function of clear command is simple — it clears all text from the terminal screen.
DESCRIPTION
clear
clears your screen if this is possible, including its scrollback buffer (if the extended “E3” capability is defined).clear
looks in the environment for the terminal type given by the environment variable TERM, and then in the terminfo database to determine how to clear the screen.
clear
writes to the standard output.
You can redirect the standard output to a file (which prevents clear from actually clearing the screen), and latercat
the file to the screen, clearing it at that point.
Manual pages
man clear
tar
- an archiving utility
DESCRIPTION
GNU tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tape archiver), which can be located either on the local or on a remote machine.
To archive a folder in .tar.gz format
tar cvzf ArchiveName.tar.gz /path/to/directory
To unpack a .tar.gz file
tar xvzf FileName.tar.gz
Notice that both commands use different four-character options — cvzf and xvzf. Each letter represents a specific instruction option.
-x, --extract, --get
Extract files from an archive. Arguments are optional. When given, they specify names of the archive
members to be extracted.
-c, --create
Create a new archive. Arguments supply the names of the files to be archived. Directories are archived
recursively, unless the --no-recursion option is given.
-v, --verbose
Verbosely list files processed.
-z, --gzip, --gunzip, --ungzip
Filter the archive through gzip(1).
-f, --file=ARCHIVE
Use archive file or device ARCHIVE. If this option is not given, tar will first examine the environment
variable `TAPE'. If it is set, its value will be used as the archive name. Otherwise, tar will assume
the compiled-in default. The default value can be inspected either using the --show-defaults option, or
at the end of the tar --help output.
Manual pages
man tar
Wget
- The non-interactive network downloader.
DESCRIPTION
GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.
Wget is non-interactive, meaning that it can work in the background, while the user is not logged on. This
allows you to start a retrieval and disconnect from the system, letting Wget finish the work. By contrast, most
of the Web browsers require constant user's presence, which can be a great hindrance when transferring a lot of
data.
Wget can follow links in HTML, XHTML, and CSS pages, to create local versions of remote web sites, fully
recreating the directory structure of the original site. This is sometimes referred to as "recursive
downloading." While doing that, Wget respects the Robot Exclusion Standard (/robots.txt). Wget can be
instructed to convert the links in downloaded files to point at the local files, for offline viewing.
Wget has been designed for robustness over slow or unstable network connections; if a download fails due to a
network problem, it will keep retrying until the whole file has been retrieved. If the server supports
regetting, it will instruct the server to continue the download from where it left off.
wget http://fileurl/filename.ext
If you want to download multiple files, put all URLs into a file and use the -i
option.
Let’s say the file containing the links is called downloads.txt
.
wget -i downloads.txt
Manual pages
man wget
du
- estimate file space usage
DESCRIPTION
Summarize disk usage of the set of FILEs, recursively for directories.
You can use du
(Disk Usage) command to view the size of files and folders in a specified directory.
du /path/to/directory
Unfortunately, the summary will show disk block numbers instead of bytes, kilobytes, and megabytes. Therefore, to show it in a human-readable format, you need to insert the -h
option after the du
command and the results will be more understandable.
du -h $HOME
The command above will list all the files and their filesizes starting in your home directory and all its sub-directories.
Learning terminal commands are an important part for proper management of Linux and UNIX server systems or VPS. Once you’ve learned it however, it will become the most effective way for you to navigate through your system and modify files or folders.
Manual pages
man du