Skip to content

1. Basic Commands

Qingye Jiang (John) edited this page Dec 12, 2021 · 1 revision

pwd

The pwd command tells you the current working directory (folder) you’re in.

ubuntu@ip-172-31-63-67:~$ pwd
/home/ubuntu

ls

The ls command shows the content in a directory. By default, this command displays the content in your current working directory.

  • ls -R will list all the files in the sub-directories as well
  • ls -a will show the hidden files
  • ls -l will list the files and directories with detailed information like the permissions, size, owner, etc.
ubuntu@ip-172-31-63-67:~$ ls

ubuntu@ip-172-31-63-67:~$ ls -a
.  ..  .bash_logout  .bashrc  .cache  .profile  .ssh

ubuntu@ip-172-31-63-67:~$ ls -l
total 0

ubuntu@ip-172-31-63-67:~$ ls -a -l
total 28
drwxr-xr-x 4 ubuntu ubuntu 4096 Jul  8 05:16 .
drwxr-xr-x 3 root   root   4096 Jul  8 05:16 ..
-rw-r--r-- 1 ubuntu ubuntu  220 Feb 25  2020 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Feb 25  2020 .bashrc
drwx------ 2 ubuntu ubuntu 4096 Jul  8 05:16 .cache
-rw-r--r-- 1 ubuntu ubuntu  807 Feb 25  2020 .profile
drwx------ 2 ubuntu ubuntu 4096 Jul  8 05:16 .ssh

You can specify the directory path you want to see the content:

ubuntu@ip-172-31-63-67:~$ ls /var
backups  cache  crash  lib  local  lock  log  mail  opt  run  snap  spool  tmp

ubuntu@ip-172-31-63-67:~$ ls -l /var
total 44
drwxr-xr-x  2 root root   4096 Apr 15  2020 backups
drwxr-xr-x 12 root root   4096 Jul  8 05:16 cache
drwxrwxrwt  2 root root   4096 Apr 30 23:25 crash
drwxr-xr-x 38 root root   4096 Jul  8 05:16 lib
drwxrwsr-x  2 root staff  4096 Apr 15  2020 local
lrwxrwxrwx  1 root root      9 Apr 30 23:15 lock -> /run/lock
drwxrwxr-x  9 root syslog 4096 Jul  8 05:16 log
drwxrwsr-x  2 root mail   4096 Apr 30 23:15 mail
drwxr-xr-x  2 root root   4096 Apr 30 23:15 opt
lrwxrwxrwx  1 root root      4 Apr 30 23:15 run -> /run
drwxr-xr-x  6 root root   4096 Apr 30 23:36 snap
drwxr-xr-x  4 root root   4096 Apr 30 23:17 spool
drwxrwxrwt  6 root root   4096 Jul  8 05:16 tmp

cd

The cd command allows you to navigate through the Linux files and directories. You can either use an absolute path (that starts with /) or a relative path (relative to the current working directory).

ubuntu@ip-172-31-63-67:~$ pwd
/home/ubuntu

ubuntu@ip-172-31-63-67:~$ ls

ubuntu@ip-172-31-63-67:~$ cd /var

ubuntu@ip-172-31-63-67:/var$ ls
backups  cache  crash  lib  local  lock  log  mail  opt  run  snap  spool  tmp

ubuntu@ip-172-31-63-67:/var$ cd log

ubuntu@ip-172-31-63-67:/var/log$ ls
amazon  auth.log  cloud-init-output.log  dist-upgrade  dpkg.log  kern.log   lastlog  syslog               wtmp
apt     btmp      cloud-init.log         dmesg         journal   landscape  private  unattended-upgrades

It should be noted that . represents current directory, .. represents the upper level directory, and ~ represents your home directory.

ubuntu@ip-172-31-63-67:/var/log$ pwd
/var/log

ubuntu@ip-172-31-63-67:/var/log$ cd .

ubuntu@ip-172-31-63-67:/var/log$ pwd
/var/log

ubuntu@ip-172-31-63-67:/var/log$ cd ..

ubuntu@ip-172-31-63-67:/var$ pwd
/var

ubuntu@ip-172-31-63-67:/var$ cd ~

ubuntu@ip-172-31-63-67:~$ pwd
/home/ubuntu

nano

On any operating system, it is important that you have the capability of editing text files. Despite the fact that vi, vim or emacs are much more famous, I myself prefers nano for its simplicity. Apart from CTRL+o for saving the buffer cache, and CTRL+x to exit the editor, you do not need to learn anything to get started.

Use the following command to create a new test file test.txt in the current directory. If test.txt already exists, nano will open the existing file for editing.

nano test.txt

As you can imagine, the command accepts either a relative path, or an absolute path. Write anything into the text file, and press CTRL+o to save the content. When you are done with editing, press CTRL+x to exit the editor.

Editing system configuration files requires *root privilege. In this case, add sudo in front of the nano command. **If you don't know what you are doing, don't mess up with things under the /etc folder."

sudo nano /etc/lsb-release

more, less, head, tail, cat

You can use more to show the content of a text file page by page. While reading, you can press SPACE to continue to the next page. If you want to exit before the last page, simply press q.

more /var/log/dmesg

When working with large files, less is a better command to use, because it does not read the whole file when starting. When working with small files, you might not see any difference between more and less.

less /usr/share/common-licenses/GPL-3

You can read the head of a text file using the head command, and read the tail of a text file using the tail command. For both head and tail, you can specify the number of lines using the -n option.

head /usr/share/common-licenses/GPL-3
head -n 20 /usr/share/common-licenses/GPL-3
tail /usr/share/common-licenses/GPL-3
tail -n 20 /usr/share/common-licenses/GPL-3

You can also use cat to dump the whole file content without stopping in the middle:

cat /usr/share/common-licenses/GPL-3

date

By default, the date command shows the current date and time with the default timezone. On an EC2 instance, the default timezone is set to UTC.

ubuntu@ip-172-31-63-67:~$ date
Mon Jul 12 05:35:57 UTC 2021

whoami

The whoami command tells you who you are.

ubuntu@ip-172-31-63-67:~$ whoami
ubuntu

who

The who command tells you the users who are currently login to the system.

ubuntu@ip-172-31-63-67:~$ who
ubuntu   pts/0        2021-07-12 04:32 (72.21.198.64)

uptime

The uptime command tells you how long the system has been up, the number of active users, and the load average during the past 1, 5 and 15 minutes. The load average is an average of the number of runnable processes over a given time period. For example, for a system with a single CPU, an hourly load average of 10 would mean that at any time during that hour one could expect to see 1 process running and 9 others ready to run (i.e., not blocked for I/O) waiting for the CPU. If you want to dive deep into this concept, please refer to Brendan Gregg's blog post Linux Load Averages: Solving the Mystery.

ubuntu@ip-172-31-63-67:~$ uptime
 05:40:08 up 3 days,  3:33,  1 user,  load average: 0.00, 0.00, 0.00

mkdir, rmdir

Use mkdir to create a new directory. Use rmdir to delete a directory. When deleting, the directory to be deleted needs to be empty.

ubuntu@ip-172-31-63-67:~$ mkdir test

ubuntu@ip-172-31-63-67:~$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 12 23:40 test
-rw-rw-r-- 1 ubuntu ubuntu    7 Jul 12 04:36 test.txt

ubuntu@ip-172-31-63-67:~$ rmdir test 

ubuntu@ip-172-31-63-67:~$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 7 Jul 12 04:36 test.txt

cp, mv, rm

Use cp to copy a file/directory to another file/directory. Use mv to move a file to another file/directory (rename) or to another directory (relocation). Use rm to delete a file/directory.

ubuntu@ip-172-31-63-67:~$ ls
test.txt

ubuntu@ip-172-31-63-67:~$ cp test.txt test.dat

ubuntu@ip-172-31-63-67:~$ ls 
test.dat  test.txt

ubuntu@ip-172-31-63-67:~$ mv test.dat test.bak

ubuntu@ip-172-31-63-67:~$ ls
test.bak  test.txt

ubuntu@ip-172-31-63-67:~$ rm test.bak

ubuntu@ip-172-31-63-67:~$ ls
test.txt

ubuntu@ip-172-31-63-67:~$ mkdir test

ubuntu@ip-172-31-63-67:~$ ls
test  test.txt

ubuntu@ip-172-31-63-67:~$ mv test.txt test

ubuntu@ip-172-31-63-67:~$ ls -l
total 4
drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 12 23:45 test

ubuntu@ip-172-31-63-67:~$ ls -l test
total 4
-rw-rw-r-- 1 ubuntu ubuntu 7 Jul 12 04:36 test.txt

touch

The touch command updates the timestamp of a file. If the file does not exist, it creates a new empty file with the current timestamp.

ubuntu@ip-172-31-63-67:~$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 12 23:45 test
-rw-rw-r-- 1 ubuntu ubuntu    6 Jul 12 23:48 test.txt

ubuntu@ip-172-31-63-67:~$ touch test.txt

ubuntu@ip-172-31-63-67:~$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 12 23:45 test
-rw-rw-r-- 1 ubuntu ubuntu    6 Jul 12 23:50 test.txt

ubuntu@ip-172-31-63-67:~$ touch test.dat

ubuntu@ip-172-31-63-67:~$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 12 23:45 test
-rw-rw-r-- 1 ubuntu ubuntu    0 Jul 12 23:50 test.dat
-rw-rw-r-- 1 ubuntu ubuntu    6 Jul 12 23:50 test.txt

man, info

Most Linux commands can accept multiple parameters to deal with very complex use cases. You do not need to remember all of them. But rather, you look into the documentation for guidance when needed. In this case, you will need the man command and the info command. The man command goes directly into the usage, while the info command give you a lot of explanations. Try to use both man and info on the same command to see the difference.

man touch
info touch
man man
man info
info man
info info

ps

The ps command displays information about a selection of active processes running on the system. By default, ps selects all processes with the same effective user ID as the current user and associated with the same terminal as the current user.

ubuntu@ip-172-31-63-67:~$ ps
    PID TTY          TIME CMD
  37209 pts/0    00:00:00 bash
  37242 pts/0    00:00:00 ps

In trouble shooting, we commonly use ps -ef to display all process running on the system, with the command that created each process.

ubuntu@ip-172-31-63-67:~$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Jul09 ?        00:00:12 /sbin/init
root           2       0  0 Jul09 ?        00:00:00 [kthreadd]
root           3       2  0 Jul09 ?        00:00:00 [rcu_gp]
root           4       2  0 Jul09 ?        00:00:00 [rcu_par_gp]
root           6       2  0 Jul09 ?        00:00:00 [kworker/0:0H-kblockd]
... ...

grep

Now you have a command that generates a lot of output (like ps -ef), you might be interested in identifying certain keywords from the output. This can be done with the grep command. The following command searches for this particular keyword ubuntu in the output.

ubuntu@ip-172-31-63-67:~$ ps aux | grep ubuntu
root       37104  0.0  0.9  13808  9068 ?        Ss   01:41   0:00 sshd: ubuntu [priv]
ubuntu     37107  0.0  0.9  18300  9416 ?        Ss   01:41   0:00 /lib/systemd/systemd --user
ubuntu     37108  0.0  0.3 103208  3344 ?        S    01:41   0:00 (sd-pam)
ubuntu     37208  0.0  0.6  13944  6036 ?        S    01:41   0:00 sshd: ubuntu@pts/0
ubuntu     37209  0.0  0.4  10056  4984 pts/0    Ss   01:41   0:00 -bash
ubuntu     37298  0.0  0.3  10624  3352 pts/0    R+   01:51   0:00 ps aux
ubuntu     37299  0.0  0.0   8168   736 pts/0    S+   01:51   0:00 grep --color=auto ubuntu

In this example, we use | to pipe the output of command ps aux as the input for command grep ubuntu. Similarly, you can search for keywords in a file, for example:

ubuntu@ip-172-31-63-67:~$ more /var/log/dmesg  | grep fail
[    1.036013] kernel: acpi PNP0A03:00: fail to add MMCONFIG information, can't access extended PCI configuration space under this bridge.

kill

Clone this wiki locally