The Linux Commands Series: Part V
type, which, nohup, xargs, vim, emacs, nano, whoami, who, su, sudo
I intend to write a series of VI blog posts describing the most used Linux Commands. This blog is the first of the series. I hope you all enjoy the reading.
Citation/References/Credit will be mentioned at the end of the post.
A command is of 4 types:
- an executable 2. a shell built-in program 3. a shell function 4. an alias
The ‘type’ command will help you to figure which category a command belongs to.
Syntax: type <command name>
This command helps you to locate where a particular command is located on the disk. This works only on executable commands. For example:
Suppose you are running a command on a remote server and you do not want the command to stop running either because of network connectivity issues between you and the server or if you want to log out and close the session, then you can use ‘nohup’ command.
Syntax: nohup <command name>
If you want to send the output of one command as input to another command then we can use ‘xargs’ command.
Syntax: command1 | xargs command2
In the above syntax, the output of command1 is sent as arguments to command2 using the pipe (|) symbol.
For example, suppose you have 4files file1, file2, file3, and file named todelete.txt with the names of those files which need to be removed then we can send the names present in todelete.txt to ‘rm’ command as below:
cat todelete.txt | xargs rm
Here, the ‘rm’ command is executed twice one for each line returned by the ‘cat’ command.
If you want a confirmation prompt asking you to confirm if you want to execute the second command before executing the command then you can use the ‘-p’ option as below:
cat todelete.txt | xargs -p rm
The above option (-p) will ask you once for confirmation for all files but if you want a confirmation prompt for each file then you can use the ‘-n’ option as below:
cat todelete.txt | xargs -p -n1 rm
Vim is a popular file editor and you can start using the command ‘vi’ on the command line.
Syntax: vi file1.txt
Vim has 2 modes:
- command mode 2. insert mode
When you start the editor, you are in command mode and cannot enter any text. You need to press ‘i’ key and you would see the ‘ — INSERT — ‘word at the bottom of the editor and you can now enter text.
You can press ESC key to exit insert mode and enter command mode. Here you can navigate using keys ‘h-j-k-l’, ‘h-j’ for left and right, and ‘k-l’ for up and down.
If you want to save the file then press ‘:w’
If you want to save and quit then press ‘:wq’
If you want to exit without saving then press ‘:q!’
You can undo and edit by going to command mode and pressing ‘u’. You can redo by pressing Ctrl+r.
You can learn more about Vim editor by typing ‘vimtutor’ on your command line which should be installed in your system already.
Emacs is also an editor as Vim. macOS users, to install the latest version using the command ‘brew install emacs’.
To open a file using emacs:
Syntax: emacs <filename>
You can start editing and once done press Ctrl-x followed by Ctrl-w. It will ask you to overwrite the file if it already exists, answer ‘y’ and it will give confirmation of success.
You can exit Emacs by pressing Ctrl-x followed by Ctrl-c.
Note: You can learn more about Emacs by entering into the Emacs session by typing ‘emacs’ on the command line and pressing Ctrl-h r for built-in manual or Ctrl-h t for the official tutorial.
Nano is also an editor and you can enter nano session using the ‘nano’ command on the command line.
You can also open a file as below:
You can exit the editor without editing by pressing Ctrl-x and you would also find commands to work with nano editor at the end of the editor.
This command prints the username who is currently logged into the terminal.
The ‘who’ command displays different users logged into the system.
You can use ‘-aH’ option to display more information like idle time and PID.
Syntax: who -aH
The command ‘who am i’ will give you current terminal session details.
Syntax: who am i
who -aH am i
When you are logged into the terminal session with a username then you can switch to a different user as below:
If you enter ‘su’ without anything else then it will prompt you to enter the root user password and it opens a new terminal as another user. You can enter exit to close the terminal and you will back to the current user’s terminal.
You can use ‘sudo’ to run a command as root.
You should have permissions to use ‘sudo’ and you can also be given permissions to use specific commands using ‘sudo’.
In order to start a shell as a root, you can type the command ‘sudo -i’.
Note: Remember to use the man command for more intel on a particular command.
This is the end of this blog post. I will publish a blog post in this series once a week.
Please follow me for more interesting content.