Learning basic Linux commands
The following table lists a few basic Linux commands:
Command Description
$ ls
This command isusedto check the content of the directory.
$ pwd
This command is used to check the present working directory.
$ mkdir work
We will work in a separate directory calledworkin ourhomedirectory. Use this command to create a new directory calledworkin the current folder.
$ cd work
This command will change our working directory to the newly createdworkdirectory.
$ pwd
This command can be used to verify whether we moved to the expected directory.
$ touch hello.sh
This command is used to create a new empty file calledhello.shin the current folder.
$ cp hello.sh bye.sh
This command is used to copy one file into another file.
This will copyhello.shasbye.sh.
$ mv bye.sh welcome.sh
This command is used to rename a file. This will renamebye.shaswelcome.sh.
$ ll
This command will display detailed information about files.
$ mv welcome.sh .welcome.sh
$ ls
Let’s see some magic. Rename the file using themvcommand and run thelscommand.
Now, thelscommand will not display our file.welcome.sh. The file is hidden. Any file or directory name starting with. (dot) becomes hidden.
$ ls -a
This command is used to display hidden files.
$ rm .welcolme.sh
This command is used to delete the file.
Note:
If we delete any file from the GUI, such as the GUI, then it will be moved to the /home/user/.local/share/Trash/files/ all deleted files folder.
Working more effectively with Shell – basic commands
Let’s learn a few commands that are required very often, such as man, echo, cat, and similar:
Enter the following command. It will show thevarioustypes of manual pages displayed by themancommand:
$ man man
From the following table, you can get an idea about various types ofmanpages for the same command:
Section number
Subject area
1
User commands
2
System calls
3
Library calls
4
Special files
5
File formats
6
Games
7
Miscellaneous
8
System admin
9
Kernel routines
We can enter themancommand to display the corresponding manual pages as follows:
$ man 1 command$ man 5 command
Suppose we need to knowmoreabout thepasswdcommand, which is used forchangingthe current password of a user. You can type the command as follows:
$ man command man -k passwd // show all pages with keyword man -K passwd // will search all manual pages content for pattern "passwd"$ man passwd
This will show information about thepasswdcommand:
$ man 5 passwd
The preceding command will give information about the filepasswd, which is stored in the/etc/folder, such as/etc/passwd.
We can get brief information about the command as follows:
$ whatis passwd
Output:passwd (1ssl) - compute password hashespasswd (1) - change user passwordpasswd (5) - the password file
Every command we type in the Terminal has an executable binary program file associated with it. We can check the location of a binary file as follows:
$ which passwd/usr/bin/passwd
The preceding linetellsus that the binary file of thepasswdcommand is located in the/usr/bin/passwdfolder.
We can get complete information about the binary file location, as well as the manual page location of any command, with the following:
$ whereis passwd
The output will be as follows:
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz /usr/share/man/man5/passwd.5.gz
Change the user login and effective username:
$ whoami
This command displays the username of the logged in user:
$ su
Thesu (switch user) command will make the user the administrator but you should know the administrator’s password. Thesudo (superuser do) command will run the command with administrator privileges. The user should have been added to thesudoerslist.
# who am i
This command will show the effective user who is working at that moment.
# exit
Many a times, you might need to create new commands from existing commands. Sometimes, existing commands have complex options to remember. In such cases, we can create new commands as follows:
$ alias ll='ls -l'$ alias copy='cp -rf'
To list alldeclaredaliases, use the following command:
$ alias
To remove an alias, use the following command:
$ unalias copy
We can check operating system details, such as UNIX/Linux or the distribution that is installed with the following command:
$ uname
Output:Linux
This will display the basic OS information (Unix name)
Linux kernel version information will be displayed by the following:
$ uname -r
Output:3.13.0-32-generic
To get all the information about a Linux machine, use the following command:
$ uname -a
Output:Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
The following commands will give you more information about the Linux distribution:
$ cat /proc/version // detailed info about distribution$ cat /etc/*release# lsb_release -a.
root@app:/home/satish# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial
The cat command is used for reading files and is displayed on the standard output.
Sometimes, we need to copy a file or directory to many places. In such situations, instead of copying the original file or directory again and again, we can create soft links. In Windows, it is a similar feature to creating a shortcut.
$ ln -s file file_link
To learn about the type of file, you can use the command file. In Linux, various types of file exist. Some examples are as follows:
- Regular file (
-) - Directory (
d) - Soft link (
l) - Character device driver (
c) - Block device driver (
b) - Pipe file (
p) - Socket file (
s)
We can get information about a file using the following command:
$ file file_name
Printing some text on screen for showing results to the user, or to ask for details is an essential activity.
The following command will create a new file calledfile_nameusing thecatcommand:
$ cat > file_name
line 1
line 2
line 3
< Cntrl + D will save the file >
But this is very rarely used, as many powerful editors already exist, such as vi or gedit.
The following command will printHello Worldon the console. Theechocommand is very useful for shell script writers:
$ echo "Hello World"
$ echo "Hello World" > hello.sh
The echo command with>overwrites thecontentof the file. If thecontentalready exists in the file, it will be deleted and new content added. In situations where we need to append the text to the file, then we can use theechocommand as follows:
$ echo "Hello World" >> hello.sh will append the text
The following command will copy the Hello World string to thehello.shfile:
The following command will display the content of the file on screen:
$ cat hello.sh
