Are you planning to switch from Windows to Linux, or have you just made the switch? Oops!!! What am I saying—whatever the reason, here you are in my world. From my past experience, when I first started using Linux, commands and the terminal scared the heck out of me. I worried about how many commands I’d need to memorize to get all my tasks done. Without a doubt, online documentation, books, man pages, and the community helped me tremendously, but I firmly believed there was an article somewhere that recorded the secret to easily learning and understanding commands. That motivated me to master Linux and make it easy to use. This article is a stepping stone to that goal.
1. ls Command
The ls command stands for List Directory Contents. Running it lists the contents of a folder, which can be files or folders.
- root@tecmint:~# ls
- Android-Games Music
- Pictures Public
- Desktop Tecmint.com
- Documents TecMint-Sync
- Downloads Templates
The “ls -l” command lists the folder contents in long listing fashion with details.
- root@tecmint:~# ls -l
- total 40588
- drwxrwxr-x 2 ravisaive ravisaive 4096 May 8 01:06 Android Games
- drwxr-xr-x 2 ravisaive ravisaive 4096 May 15 10:50 Desktop
- drwxr-xr-x 2 ravisaive ravisaive 4096 May 16 16:45 Documents
- drwxr-xr-x 6 ravisaive ravisaive 4096 May 16 14:34 Downloads
- drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Music
- drwxr-xr-x 2 ravisaive ravisaive 4096 May 9 17:54 Pictures
- drwxrwxr-x 5 ravisaive ravisaive 4096 May 3 18:44 Tecmint.com
- drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Templates
The “ls -a” command lists all contents of the folder, including hidden files starting with ".".
- root@tecmint:~# ls -a
- . .gnupg .dbus .goutputstream-PI5VVW .mission-control
- .adobe deja-dup .grsync .mozilla .themes
- .gstreamer-0.10 .mtpaint .thumbnails .gtk-bookmarks .thunderbird
- .HotShots .mysql_history .htaccess .apport-ignore.xml .ICEauthority
- .profile .bash_history .icons .bash_logout .fbmessenger
- .jedit .pulse .bashrc .liferea_1.8 .pulse-cookie
- .Xauthority .gconf .local .Xauthority.HGHVWW .cache
- .gftp .macromedia .remmina .cinnamon .gimp-2.8
- .ssh .xsession-errors .compiz .gnome teamviewer_linux.deb
- .xsession-errors.old .config .gnome2 .zoncolor
Note: In Linux, files beginning with a “.” are hidden files, and every file, folder, device, or command is treated as a file. The ls -l command outputs:
- d (indicates a directory).
- rwxr-xr-x represents the permissions of the file or directory for the owning user, the owning group, and other users.
- In the example above, the first ravisaive indicates the file belongs to the user ravisaive.
- The second ravisaive in the example indicates the file belongs to the group ravisaive.
- 4096 represents the file size being 4096 bytes.
- May 8 01:06 represents the date and time of the file’s last modification.
- The final part is the file/directory name.
2. lsblk Command
“lsblk” stands for list block devices. It neatly displays block devices (excluding RAM) in a standard tree-like output format.
- root@tecmint:~# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 232.9G 0 disk
- ├─sda1 8:1 0 46.6G 0 part /
- ├─sda2 8:2 0 1K 0 part
- ├─sda5 8:5 0 190M 0 part /boot
- ├─sda6 8:6 0 3.7G 0 part [SWAP]
- ├─sda7 8:7 0 93.1G 0 part /data
- └─sda8 8:8 0 89.2G 0 part /personal
- sr0 11:0 1 1024M 0 rom
The “lsblk -l” command displays block devices in a list format (instead of a tree format).
- root@tecmint:~# lsblk -l
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 232.9G 0 disk
- sda1 8:1 0 46.6G 0 part /
- sda2 8:2 0 1K 0 part
- sda5 8:5 0 190M 0 part /boot
- sda6 8:6 0 3.7G 0 part [SWAP]
- sda7 8:7 0 93.1G 0 part /data
- sda8 8:8 0 89.2G 0 part /personal
- sr0 11:0 1 1024M 0 rom
Note: lsblk is one of the most useful and simplest ways to learn the name of a newly inserted USB device, especially when you are working with disks/block devices in the terminal.
3. md5sum Command
“md5sum” calculates and verifies MD5 message digests. The md5 checksum (often called a hash) is used to match or verify the integrity of files, as files may change due to transmission errors, disk errors, or unintentional interference.
- root@tecmint:~# md5sum teamviewer_linux.deb
- 47790ed345a7b7970fc1f2ac50c97002 teamviewer_linux.deb
Note: Users can compare the official provided signature with the one generated by md5sum to check if a file has changed. Md5sum is not as secure as sha1sum, which we will discuss later.
4. dd Command
The “dd” command stands for convert and copy a file. It can be used to convert and copy files, most commonly to copy an iso file (or any other file) to a USB device (or anywhere else), thus it can be used to create a bootable USB drive.
- root@tecmint:~# dd if=/home/user/Downloads/debian.iso of=/dev/sdb1 bs=512M; sync
Note: In the example above, the USB device is sdb1 (you should verify it using the lsblk command, otherwise you might overwrite your disk or system). Please use the disk name with extreme caution.
The dd command execution time varies from a few seconds to several minutes depending on the file size, type, and the read/write speed of the USB device.
5. uname Command
"uname" is short for Unix Name. It displays detailed information about the machine name, operating system, and kernel.
- root@tecmint:~# uname -a
- Linux tecmint 3.8.0-19-generic #30-Ubuntu SMP Wed May 1 16:36:13 UTC 2013 i686 i686 i686 GNU/Linux
Note: uname displays the kernel type, and uname -a displays detailed information. The output above details uname -a
- “Linux“: The machine’s kernel name
- “tecmint“: The machine’s node name
- “3.8.0-19-generic“: The kernel release version
- “#30-Ubuntu SMP“: The kernel version
- “i686“: The processor architecture
- “GNU/Linux“: The operating system name
6. history Command
The “history” command stands for history. It displays the history of all commands executed in the terminal.
- root@tecmint:~# history
- 1 sudo add-apt-repository ppa:tualatrix/ppa
- 2 sudo apt-get update
- 3 sudo apt-get install ubuntu-tweak
- 4 sudo add-apt-repository ppa:diesch/testing
- 5 sudo apt-get update
- 6 sudo apt-get install indicator-privacy
- 7 sudo add-apt-repository ppa:atareao/atareao
- 8 sudo apt-get update
- 9 sudo apt-get install my-weather-indicator
- 10 pwd
- 11 cd && sudo cp -r unity/6 /usr/share/unity/
- 12 cd /usr/share/unity/icons/
- 13 cd /usr/share/unity
Note: Pressing “CTRL + R” allows you to search through previously executed commands, and it offers auto-completion as you type.
- (reverse-i-search)`if': ifconfig
7. sudo Command
The “sudo” (super user do) command allows authorized users to execute commands as the superuser or another user, as specified by the security policy in the sudoers list.
- root@tecmint:~# sudo add-apt-repository ppa:tualatrix/ppa
Note: sudo allows a user to borrow superuser privileges, whereas the "su" command actually allows a user to log in as the superuser. Therefore, sudo is safer than su.
It is not recommended to use sudo or su for everyday tasks, as it could lead to serious errors if you accidentally do something wrong. That is why there is a popular saying in the Linux community:
| “To err is human, but to really foul up everything, you need root password.” |
8. mkdir Command
The “mkdir” (Make directory) command creates a new directory at the specified path. However, if the directory already exists, it will return an error message "cannot create folder, folder already exists".
- root@tecmint:~# mkdir tecmint
Note: Directories can only be created in locations where the user has write permissions. mkdir: cannot create directory `tecmint` because the file already exists. (Don’t be confused by the word ‘file’ in the output above—remember what I said at the beginning: in Linux, files, folders, drivers, commands, and scripts are all treated as files).
9. touch Command
The “touch” command updates the access and modification times of a file to the current time. The touch command only creates a file if it does not already exist. If the file exists, it updates the timestamp but does not change the file’s content.
- root@tecmint:~# touch tecmintfile
Note: touch can be used to create new, non-existent files in directories where the user has write permissions.
10. chmod Command
The “chmod” command changes the file mode bits. chmod modifies the permissions of each given file, folder, script, etc., according to the specified mode.
There are three types of permissions for files (or folders and others; for simplicity, we’ll just say files).
- Read (r)=4
- Write(w)=2
- Execute(x)=1
So if you want to give a file read-only permission, set it to '4'; write-only, set to '2'; execute-only, set to 1; read and write, that is 4+2 = 6, and so on.
Now, you need to set permissions for three types of users and groups. First is the owner, then the group the user belongs to, and finally, other users.
- rwxr-x–x abc.sh
Here, the root permissions are rwx (read, write, and execute).
The owning group permissions are r-x (read and execute only, no write permission).
For other users, the permissions are -x (execute only).
To change its permissions and provide read, write, and execute access to the owner, group, and other users:
- root@tecmint:~# chmod 777 abc.sh
All three with only read and write permissions:
- root@tecmint:~# chmod 666 abc.sh
Owner has read, write, and execute permissions; the user’s group and other users have execute-only permission:
- root@tecmint:~# chmod 711 abc.sh
Note: This is one of the most useful commands for system administrators and users alike. In a multi-user environment or on a server, if a file is set to be inaccessible for a specific user, this command can resolve it. Conversely, if incorrect permissions are set, it could provide unauthorized access.