10 Useful Commands for Linux Newbies
Let me guess!! You are planning to switch from Windows to Linux, or have just switched to Linux 🙂 Or are you a tech savvy student curious to explore more in Linux Thinking how I knew? Otherwise you woudn’t have been here 🙂 No doubt online documentation, books, man pages and user community helped me a lot but I strongly believed that there should be an article with details of commands in easy to learn and understand language. But most articles are ambiguous and do the same thing in different ways. So referring to multiple articles for learning how to do something is quite confusing. So this is why I decided to write an article to help beginners with the basic commands to be used. I have taken extra care to point the commands which I found confusing and are necessary. So lets skip the introductions and start. 😉
1. Command: ls
The “ls” command stands for (List Directory Contents), The full name of the command explains what it does. Thats right 🙂 It lists the contents of the folder, be it file or folder, from which it runs.
Dont like the way the output is listed intwo row column fasion. Good, that makes two of us 🙂 Issue the command “ls -l” to list the content of folder, in long listing fashion.
drwxrwxr-x 2 user1 user1 4096 May 8 01:06 Games
drwxr-xr-x 2 user1 user1 4096 May 15 10:50 Desktop
drwxr-xr-x 2 user1 user1 4096 May 16 16:45 Documents
drwxr-xr-x 6 user1 user1 4096 May 16 14:34 Downloads
drwxr-xr-x 2 user1 user1 4096 Apr 30 20:50 Music
drwxr-xr-x 2 user1 user1 4096 May 9 17:54 Pictures
drwxrwxr-x 5 user1 user1 4096 May 3 18:44 Blog
drwxr-xr-x 2 user1 user1 4096 Apr 30 20:50 Templates
Notice something? The hidden files (with . in the beginning) are not displayed. How do we display that? Well, run the command as “ls -a“, it lists the content of folder, including hidden files starting with ‘.’.
. .gnupg .dbus .goutputstream .mission-control
.adobe deja-dup .grsync .mozilla .themes
.gstreamer-0.10 .mtpaint .thumbnails .gtk-bookmarks .thunderbird
.HotShots .mysql .htaccess .apport-ignore.xml .ICEauthority
Note: In Linux file name starting with ‘.‘ is hidden. In Linux every file/folder/device/command is a file. The output of ls -l is:
- d (stands for directory).
- rwxr-xr-x is the file permission of the file/folder for owner, group and world.
- The 1st user1 in the above example means that file is owned by user user1.
- The 2nd user1 in the above example means file belongs to user group user1.
- 4096 means file size is 4096 Bytes.
- May 8 01:06 is the date and time of last modification.
- And at the end is the name of the File/Folder.
2. Command: lsblk
The “lsblk” stands for (List Block Devices), print block devices by their assigned name on the standard output in a tree-like fashion.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 154.9G 0 disk
├─sda1 8:1 0 36.6G 0 part /
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 180M 0 part /boot
├─sda6 8:6 0 1.3G 0 part [SWAP]
├─sda7 8:7 0 93.1G 0 part /data
sr0 11:0 1 1024M 0 rom
3. Command: md5sum
The “md5sum” stands for (Compute and Check MD5 Message Digest), md5 checksum (commonly called hash) is used to match or verify integrity of files that may have changed as a result of a faulty file transfer, a disk error or non-malicious interference.
4. Command: dd
The dd command copies a file, converting the format of the data in the process, according to the operands specified. It is generally used to copy a iso file (or any other file) to a usb device (or any other location), thus can be used to make a ‘Bootable‘ Usb Stick.
Note: In the above example the usb device which is to be made bootable is sdb2 ( Verify it using command lsblk, as otherwise you will may end up overwriting your disk and OS which clearly is not good)
dd command takes some time ranging from a few seconds to several minutes in execution, depending on the size and type of file and read and write speed of Usb stick.
6. Command: history
The “history” command stands for History (Event) Record, it prints the history of long list of executed commands in terminal.
1 vi environment.rb
3 vi /etc/redmine/default/configuration.yml
4 mv configuration.yml.example configuration.yml
5 vi configuration.yml
6 /etc/init.d/apache2 restart
Note: Pressing “Ctrl + R” and then search for already executed commands which lets your command to be completed with auto completion feature.
(reverse-i-search)`make’: make install
7. Command: mkdir
The “mkdir” (Make directory) command create a new directory with name path. However is the directory already exists, it will return an error message “cannot create folder, folder already exists”.
Note: Directory can only be created inside the folder, in which the user has write permission. mkdir: cannot create directory `testdir‘: File exists
(Don’t confuse with file in the above output, you might remember what i said at the beginning – In Linux every file, folder, drive, command, scripts are treated as file).
8. Command: touch
The “touch” command stands for (Update the access and modification times of each FILE to the current time). touch command creates the file, only if it doesn’t exist. If the file already exists it will update the timestamp and not the contents of the file.
Note: touch can be used to create file under directory, on which the user has write permission, only if the file don’t exist there.
9. Command: chmod
The Linux “chmod” command stands for (change file mode bits). chmod changes the file mode (permission) of each given file, folder, script, etc.. according to mode asked for.
There exist 3 types of permission on a file (folder or anything but to keep things simple we will be using file).
So if you want to give only read permission on a file it will be assigned a value of ‘4‘, for write permission only, a value of ‘2‘ and for execute permission only, a value of ‘1‘ is to be given. For read and write permission 4+2 = ‘6‘ is to be given, ans so on.
Now permission need to be set for 3 kinds of user and usergroup. The first is owner, then usergroup and finally world.
Here the root’s permission is rwx (read, write and execute).
usergroup to which it belongs, is r-x (read and execute only, no write permission) and
for world is –x (only execute).
To change its permission and provide read, write and execute permission to owner, group and world.
only read and write permission to all three.
read, write and execute to owner and only execute to group and world.
Note: one of the most important command useful for sysadmin and user both. On a multi-user environment or on a server, this command comes to rescue, setting wrong permission will either makes a file inaccessible or provide unauthorized access to someone.
10. Command: chown
The Linux “chown” command stands for (change file owner and group). Every file belongs to a group of user and a owner. It is used Do ‘ls -l‘ into your directory and you will see something like this.
drwxr-xr-x 3 server root 4096 May 10 11:14 Binary
drwxr-xr-x 2 server server 4096 May 13 09:42 Desktop
Here the directory Binary is owned by user “server” and it belongs to usergroup “root” where as directory “Desktop” is owned by user “server” and belongs to user group “server“.
This “chown” command is used to change the file ownership and thus is useful in managing and providing file to authorised user and usergroup only.
drwxr-xr-x 3 server server 4096 May 10 11:14 Test
drwxr-xr-x 2 server server 4096 May 13 09:42 Desktop
Note: “chown” changes the user and group ownership of each given FILE to NEW-OWNER or to the user and group of an existing reference file.