About technologies for your digital home

For beginners and tinkerers

Basic Linux Commands

May 22nd, 2014

When I was new to Linux, basic Linux commands looked very scary and the list of them looked endless as well… ūüėČ I started learning them bit by bit until I realize one day, that I do not use many¬†of them so often. That was a practical example of Pareto principle, which is also known as¬†the 80/20 rule.

So I decided to write a post about my 20 basic Linux commands for doing some useful stuff on Linux system. The list is not exhaustive, however it might be useful for someone just beginning learn Linux.

File Commands

1st command: ls

Command ls stands for list files & folders. I prefer to use it as:

ls -lah

because it returns more comprehensive information about your files and directories compared just to ‘plain’ command ls. The output of ls -lah will look like:

ls-lah-command-output

The 1st column from the left shows the type (file or directory) as ‘‘ or ‘d‘ marked in red. Next follows the steps of read, write and execute permissions (wrx) for the owner, the group and others, which are marked in blue.

All files and directories on Linux OS have (at least) their owners and may have assigned groups. Assigned owners and the groups are shown in 2nd column. They are marked in green.

The 3rd column shows the file or directory size in “human” format: KB, MB or GB but not pure bytes ūüėČ

The 4th column displays file (directory) creation date. The last column is file or directory name itself.

If you enter the command ls, the result will be for the current directory. Adding the path to the command ls:

ls -lah /home/

will show the contents of your specified dir ‘/home/’ independently of your position in Linux file system:

2nd command: cd

Command cd stands for change directory. Basically it allows you to move within Linux file system one directory down or up:

$ cd /game

or one up by issuing ‘

cd ..'

If you want to go up few directories, you need to issue to issue the command ‘

cd ../..

And the last cd command trick is ‘cd ~‘ ¬†will bring back to your Linux user home directory:

$ cd ~

3rd command: mkdir

Command mkdir stands for making directory. If you will add directory name after mkdir command:

$ mkdir DIR_NAME

It will create a directory in current directory. The command¬†mkdir can be used with ~,¬†in that case new directory will be created under your user’s home directory:

$ mkdir ~/DIR

4th command pwd

The command pwd returns present working directory.
Just enter:

$ pwd

and you will where you are ūüėČ

5th command rm

The command can remove either a file or a directory:
This command will delete a file:

$ rm file.txt

or some files with the extension *.txt

$ rm *.txt

and this one will delete a directory

$ rm -r dir_name

6h command cp

This command allows you to copy file1 to file2

cp file1 file2

or copy some type files to a new location

cp *.txt /new_path/new_path2

or copy recursively the directory and its contents:

cp -r dir1 dir2

7th command mv

Rename or move the files
It will rename the file:

$mv file1 file2

or it will move

$mv file1 /new_path/

8th command cat

It allows to view the file contents in the terminal:

cat > file ‚Äď places standard input into file

9th command tail

The command output the last 10 lines of file:

tail file.txt

or it can output the contents of file as it grows.

tail -f file.txt

File Permissions

10th command chmod

The command chmod is used to change the permissions on the file(s) or the directory(s).

One of the ways to assign the permissions mask is to use three numbers set. The permissions can be write, read and execute (as it was shown in command ls  case at the beginning of this post).

Here is an example of how to assign read, write and execute permissions to the owner, the group and other:

chmod 777 filename

Hmm, yes, it looked somewhat weird, that in order to assign the permissions on the files, you have to assign the number ūüėČ

Here is a nice visualization for better memorizing the number mask for Linux file permissions, which I hope will help you better understand how that works ūüėČ

If you want to assign separately:

  • no permissions you should use 0 (e.g. theoretical mask 000);
  • for execute you should use 1 (e.g. mask 111);
  • for write you should use 2 (e.g. mask 222);
  • for read ¬†you should use 4 (e.g. mask 444).

In the real life, you will use some combination of the permissions:

read(4)+execute(1) permissions => 555;

read(4) +write(2)+execute(1) permissions =>777;

As you could see, you just add together the masks associated with different permissions.

In order to recursively apply the permissions, you have to use argument -R:

chmod -R 777 DIR_NAME

Process Management

11th command top

This command displays your currently active processes, like:

Mem: 499888K used, 2864188K free, 0K shrd, 166197704K buff, 166197752K cached
CPU:  0.0% usr  2.3% sys  0.0% nic 97.6% idle  0.0% io  0.0% irq  0.0% sirq
Load average: 0.02 0.05 0.05 2/138 884
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
  744     1 root     S     282m  8.5   3  0.0 /usr/lib/kodi/kodi.bin 
  364   342 root     S    47984  1.4   2  0.0 /usr/bin/Xorg vt01 -s 0
  218     1 root     S     7872  0.2   1  0.0 /usr/lib/systemd/systemd-udevd
  879   338 root     S     7368  0.2   0  0.0 sshd: root@pts/0
.....................................................................

As you might, there is plenty of useful information including the process id (pid)!

12th command is kill

The processes occasionally hang even on Linux. This command is used kill command to terminate a process:

kill pid

where pid is from the command top output.

Typically, you have sudo command in front of kill in order to kill the process.

System/System info

13th command: sudo

Certain commands are permitted to run only for root. Command sudo is used¬†to execute a¬†command¬†as the superuser instead of using root account. The current user needs to belong to the¬†sudoers¬†group in order to be able use the command sudo. Here is an example of non-successful execution of the command¬†apt-get without¬†sudo: Linux basic command sudo apt-get and here is an example of the command ‘sudo apt-get update‘: Linux basic command sudo apt-get update

14th command: apt-get

The command apt-get is great for handling (adding, removing, updating etc.) Debian/Ubuntu Linux packages. Here is a list of the main options: install

apt-get install package_1 package_2 ... package_n

This will install additional packages onto your Linux system.

install -f

apt-get install -f

The command will install missing packages e.g. which were required during the installation of other packages.

remove

apt-get remove package_1 package_2 ... package_n

This will remove selected packages from your system.

update

apt-get update

This command will update/resynchronize the package index files from their sources dependent on your location specified in the file /etc/apt/sources.list.

upgrade

apt-get upgrade

This command will install the newest versions of the packages on your system.

dist-upgrade

apt-get upgrade

It will upgrade your Linux into the newest one.

autoremove

apt-get upgrade

This command will remove all not required packages, which were installed only as a requirement for (an)other package(s).

15th command whoami

This command displays the current logged in user at your console:

$whoami

It might sound a bit weird, but it might be also useful if you are logged as root user.

16th command man

man cat

Displays the manual page (instead of googling) of a specific command.

17th command du

The command is used to show directory space usage or to check disk space usage.
To show check disk space usage:

$ du -h

or to show directory space usage:

$ du -h /path/

18th command date

As the name implies itself, the command shows the current date and time.

$date

or it used to set the date:

date -s "01/31/2020 23:59:59"

19th command uptime

This command is used show current uptime, which can be important to know.

21:06:13 up  1:48,  load average: 0.31, 0.21, 0.12

20th HW information

Well, you can see already mentioned command cat earlier.

It can show cpu information:

cat /proc/cpuinfo

and the memory information:

cat /proc/meminfo

Hope you will be better with using basic Linux commands.