Working more effectively with shell – basic commands
Let us learn a few commands, which are required very often, such as man, echo, cat and similar:
Enter the following command. It will show the various types of manual pages displayed by the
mancommand:$ man manFrom the following table, you can get an idea about various types of
manpages 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 the
mancommand to display corresponding manual pages as follows:$ man 1 command $ man 5 command
Suppose we need to know more about the
passwdcommand, which is used for changing the 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 for pattern $ man passwd
This will show information about the
passwdcommand:$ man 5 passwdThe preceding command will give information about the file
passwd, which is stored in/etc /passwd.We can get brief information about the command as follows:
$ whatis passwdOutput:
passwd (1ssl) - compute password hashes passwd (1) - change user password passwd (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 line tells us that the binary file of the
passwdcommand is located in the/usr/bin/passwdfolder.We can get complete information about the binary file location as well as manual page location of any command by following:
$ whereis passwdThe 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.gzChange the user login and effective user name:
$ whoamiThis command displays the user name of the logged in user:
$ suThe
sucommand (switch user) will make the user as the administrator; but, you should know the administrators, password. Thesudocommand (superuser do) will run the command with administrator's privileges. It is necessary that the user should have been added in thesudoerslist.# who am iThis command will show the effective user who is working at that moment.
# exitMany 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 all declared aliases, use the following command:
$ aliasTo remove an alias, use the following command:
$ unalias copyWe can check about the operating system details such as UNIX/Linux or the distribution that is installed by the following command:
$ unameOutput:
LinuxThis will display the basic OS information (UNIX name)
Linux kernel version information will be displayed by the following:
$ uname –rOutput:
3.13.0-32-genericTo get all the information about a Linux machine, use the following command:
$ uname –aOutput:
Linux ubuntu 3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul 15 03:50:54 UTC 2014 i686 i686 i386 GNU/LinuxThe following commands will give you more information about the distribution of Linux:
$ cat /proc/version // detailed info about distribution $ cat /etc/*release # lsb_release -a // will tell distribution info for Ubuntu
The command
catis used for reading files and displayed on the standard output.Sometimes, we need to copy a file or directory in many places. In such situations, instead of copying the original file or directory again and again, we can create soft links. In Windows, a similar feature is called as creating a shortcut.
$ ln -s file file_linkTo learn about the type of file, you can use the command file. In Linux, various types of files 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 fil_name // show type of filePrinting some text on the screen for showing results to the user or to ask details is an essential activity.
The following command will create a new file called
file_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 are already existing, such as vi or gedit.
The following command will print
Hello Worldon the console. Theechocommand is very useful for Shell script writers:$ echo "Hello World"The following command will copy the string
Hello Worldto thehello.cfile:$ echo "Hello World" > hello.cThe command
echowith>overwrites the content of the file. If content already exists in the file, it will be deleted and new content will be added in the file. In a situation, when we need to append the text to the file, then we can use theechocommand as follows:$ echo "Hello World" >> hello.c will append the textThe following command will display the content of the file on screen:
$ cat hello.c