Ubuntu 18.04 Terminal Commands · GitHub
Ubuntu 18.04 Terminal Commands · GitHub
Here are some words that we use and some meanings that you may associate them with.
directory folder
terminal Shell
echo print
Sudo
user@my_computer_name:~$ sudo
apt-get
This command is used to install, update, upgrade, and remove any package. Examples below will show how to use in
different ways:
superuser privileges. This command updates your system along with the packages that are installed.
This will upgrade the packages that have available updates. You can also upgrade just one package by adding a space
along with the package name after upgrade.
sudo apt-get install package name you wish to install You would replace the package-name with the name of the
program you wish to install.
echo
echo is that useful of a command but we are going to use it to help us learn about command line arguments. In the
example below each word in the command line is an argument.
user@my_computer_name:~$ echo Each word I enter in the command line is an argument.
Each word I enter in the command line is an argument.
But if we wrap " " or ' ' around the sentence then that sentence would be just one argument. Example:
The quotes " " or ' ' are ignored by the terminal (shell) but the sentence is counted as just one argument. This is good to
learn because if we have a file or directory with spaces in it's name and we want to use that file or directory. We will have
to use quotes " " or ' ' to tell the terminal to use it. If we did not and we try to use a command cat to see the content of a
file and the file has spaces in it's name. The command would throw an error. So, in order to not throw an error we use
quotes to make the file or directory as one argument. Example:
ls
(list) command will list out the files in the current working directory you are in.
user@my_computer_name:~$ ls
Desktop Documents Webdev Dev
You can also look at different files in other directories by adding a space then the direct path to the other directory you
wish to look into.
user@my_computer_name:~$ ls Dev
Python JS HTML CSS
user@my_computer_name:~/Dev$ ls -a
.I_was_hiding Python JS HTML CSS .hidden.txt
This command will list all the files in that directory including hidden files.
ls -t
This list command will return the directories or files in their time stamp order. This ls -t command can also be combined
with -a. This combination would return a list of the hidden directories or files in their time stamp order. Example:
user@my_computer_name:~/Dev$ ls -a -t
HTML CSS JS .hidden.txt Python .I_was_hiding
whoami
user@my_computer_name:~$ whoami
user
cd
(change directory) used to change the current working directory. You use it by typing cd space then the file name within
your current directory or full path to another directory and/or file if in a different directory. Examples of different ways
below:
cd /
user@my_computer_name:~Dev/Python$ cd /
user@my_computer_name:/$
This will take me to the root directory. Note that the ~ tilde is not in the return command line.
cd ..
user@my_computer_name:~/Dev/JS Practice$ cd ..
user@my_computer_name:~/Dev$
This will take me from the JS Practice to it's parent directory Dev. Note that JS Practice has a space. This is not a
proper way to name a file
In the example above: When I am in the Dev directory and want to access the file JS Practice I can do this two
ways:
Both command lines would produce the same results. They would have cd (changed directory) in to JS Practice.
But try not to name files with spaces in them. Use an _ underscore instead of a space.
cd -
user@my_computer_name:~/Dev$ cd -
user@my_computer_name:~/Dev/JS Practice$
cd ~
user@my_computer_name:~/Dev/JS Practice$ cd ~
user@my_computer_name:~$
We used cd to change directory but in the examples above we used their relative path. Meaning the path we went to was
relative to the parent directory. While using absolute paths, we start from the root directory and go down to the location
you want to go. Examples below:
Relative Path
user@my_computer_name:~$ ls
Desktop Documents Dev Webdev
user@my_computer_name:~$ cd Dev/PracticePython
user@my_computer_name:~/Dev/PracticePython$
Absolute Path
user@my_computer_name:~$ ls /
bin dev sys home
user@my_computer_name:~/home$ ls
user
user@my_computer_name:~$ cd /home/user/Dev/PracticePython
user@my_computer_name:~/Dev/PracticePython$
pwd
(print working directory) This will display the full pathname of the working directory.
user@my_computer_name:~Dev/Python/Practice$ pwd
/home/user/Dev/Python/Practice
cp
(copy) - After typing cp you type the file you want to copy + space + location you want to place the copied file.
This will move the pyproject.py file in my current directory to the Python_Project directory. You can also move
directories using this command.
rm
(remove) This command will remove the specified file but not the directory.
user@my_computer_name:~/Dev/Webdev$ rm myApp.js
If there is no error, means it worked
rmdir
rm -r
(remove recursively) - Removes a directory along with its content. This will remove a non-empty directory
user@my_computer_name:~/Dev$ rm -r No_Longer_Needed_Practice_File
If there is no error, means it worked
rm -f
(remove force) This will forcefully remove files. But to forcefully remove non-empty dirctories use:
rm -i
user@my_computer_name:~/Dev/Python/Practice$ rm -i basic.py
rm: remove regular file? <You enter y(yes) or n(no) then press enter>
mkdir
(make directory) This command will allow you to create a new directory. You can also specify where you want to create
this directory's location by putting a space followed by the location behind mkdir. Example:
To Make File
To make a file in your current directory do:
This will open up the nano program, built into Terminal, with the file_name_you_want. (More on nano is explained below)
ctrl+c
control + c Now that we are dealing with more argument commands we need to learn how to break out of them. If you
are in a command argument and for some reason you want to break out of it. You would use ctrl+c
user@my_comuter_name:~$ if [Dev]; then echo "There is a Dev directory" else echo "There is not a Dev
directory" <you hit the enter key but forgot the closing fi>
> <but the command line is still waiting for more input. So, you hit enter again>
> <and again because you forgot how to end and want to start over>
>
> <You can cancel the conditional statement by> ctrl+C
^C
user@my_computer_name:~$
As we can see from the command line that went we used the ctrl+c that the terminal displays ^C. This is normal. That is
the output of our command ctrl+c. As you will see when using the command that it breaks you out of the argument or
conditional statement in our example and returns your active command line back, ready for you to enter any new
commands.
locate
Use this command to find files and their location by their name. If you are not sure if the file name is upper case or lower
case use -i to ignore the case. If you wish to locate two or more words at a time you can seprate each word with (*).
Example below:
If you just want to have a count of the times the word is in the directories you can use -c. Example:
This would return a number of how many times the word Python is in the directories.
This will ignore the case and return a count for how many times Python and/or python is in the directories.
find
This command will search the current directory and sub-directories. You can also use the wild card command with the
option -name to find extentions. Example below.
history
This command will display all of your previous commands up to the history memory limit.
user@my_computer_name:~\$ history
1 cd ~
2 top
3 man top
and so on...
df
(display filesystem) This will display information about your disk space usage of your filesystem.
user@my_computer_name:~\$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 5846980 0 5846980 0% /dev
tmpfs 1180420 2072 1178348 1% /run
/dev/sda5 479122128 33007000 421707372 8% /
and so on...
user@my_computer_name:~\$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 5.6G 0 5.6G 0% /dev
tmpfs 1.2G 2.1M 1.2G 1% /run
/dev/sda5 457G 32G 403G 8% /
and so on...
free
This command displays the amount of free space available on the system.
user@my_computer_name:~\$ free
total used free shared buff/cache available
Mem: 11804196 2661544 7855232 56584 1287420 8985272
Swap: 2097148 0 2097148
user@my_computer_name:~\$ free -h
total used free shared buff/cache available
Mem: 11G 2.5G 7.5G 55M 1.2G 8.6G
Swap: 2.0G 0B 2.0G
top
This will display the processes using the most resources. You can exit out by typing q
user@my_computer_name:~\$ top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2298 user 20 0 684664 124244 95140 S 31.5 1.1 12:35.16 chrome
2632 user 20 0 1009140 245928 100964 R 31.5 2.1 15:01.70 chrome
and so on ...
q
This is just a small example of the power of top. To get a full understanding of top I suggest using info top
cat
Displays the contents of a file. We saw an example of this at the begining of this cheatsheet.
We can also have cat return our content with number line printout. We can do this by using the -n with our cat command.
Example:
1. def helloWorld():
2. print("Hello World!")
less
This command is like the cat command but the differents is that cat will display all your content at once while less shows
you the top of the file that will fill the terminal screen and you can use the space bar to move down line by line or use the
down arrow. less also lets you move back up with the up arrow. And when you reach the bottom of the content, less will
not quite the command which lets you move back up if you want. But if you are done you must press q to quit out of less.
Unlike cat less will remove the content and allows you to see your previous command lines. Which I like because it seems
less cluttery.
To save space I will not show example that will fill the full screen.
# When ran the print out will say Hello <your name>
Long example but I think you get the picture of how the command less works. When you have ended the content, END
will be printed. You can excape from the less command by pressing q.
man
(manual) This command will give you a description of any command that come after the command man. Example:
NAME
install - copy files and set attributes
SYNOPSIS
install [OPTION]... [-T] SOURCE DEST
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY...
...
info
(information) This command is a lot like man but the different is that info goes into more information about the
command. Example:
NAME
apt-get - APT package handling utility -- command-line interface
SYNOPSIS
apt-get [-asqdyfmubV][-o=config_string] [-c=config_file][-t=target_release] [-a=architecture] {update |
upgrade |
dselect-upgrade | dist-upgrade |
...
Ubuntu 18.04 does not have the command rename installed but can be installed using:
But the Ubuntu community recommands using mv command. Let's say I have a file in my Dev directory I want to rename.
user@my_computer_name:~\$ mv /home/user/Dev/Basic_Terminal_Commands/basic_commands.md
/home/user/Dev/Basic_Terminal_Commands/ubuntu_terminal_commands.md
If there is no error, means it worked
user@my_computer_name:~$ for file in <the file or dir you want to rename>; do mv $file \${file//<name of the
file or dir you want to rename>/<the new name you want>}; done
If there is no error, means it worked
clear
clear will clear everything off your terminal display except your active directory/file command line.
user@my_computer_name:~\$ clear
Ctrl+l
This command is the same as clear command but it just a shortcut command.
Nano
In most Unix systems they have an user friendly editor program called nano. Nano is very straight forward to write in. To
use the editor use the command nano. See example below
def hello():
print("Hello Command line")
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Linter ^_ Go To Line
As we see in this example. In order to save our info in the editor we must use the Ctrl(Control key) plus O and Ctrl+X to
Exit nano.
Shortcuts Functions
Ctrl + R Searches your history for commands matching what you had typed
Ctrl + Z Temporarily stops the active process by sending the signal SIGSTOP
Scrolls through your command history, This can allow you to quickly execute your
Up/Down Arrow Keys
previous command multiple times
Can complete the command you are typing. Can also be use to complete mulitple
TAB
commands in a row.