Linux Commands
This page sheds light on commands which may not be known to newbies of Linux OS
Last updated
This page sheds light on commands which may not be known to newbies of Linux OS
Last updated
Just like apt (the package manager) is used to install services, "apt purge" can be used to remove the same.
Just like cat command is used to display the contents of the file, if suppose line number is also required against the content, then nl is the command for it
It is simply reverse of cat.
?, *, [], [!] are some of the wildcards that can be used in linux. The ? wildcard represents one character, the * represents any number of characters, the list of characters contained in the [] represents one character, but the character must be a member of the list, and the list of characters contained in [!] represents one character, but the character cannot be a member of the list.
As per help section, sort has the following options:
who is the command which is equivalent of "query user" or "qwinsta" in Windows, which basically tells the currently logged in users. Similarly, last is used to check the last logged on users, basically the login history. Lastly w tells the same information as who but with more details.
Xargs is an utility that can simplify the processing of complex operations, repetitive tasks, batch processing, and large data manipulation. Xargs reads data from stdin, and then executes the command supplied to it as an argument. By default, xargs reads data separated by blanks because it treats spaces as delimiter, and then executes a command once for each argument. Note that blank lines are ignored.
These commands can use the following operands:
test is used to check file types and compare values and returns true or false. Another way to use test is to use the construct [...]
expr command evaluates arithmetic or string expressions and sends the result to stdout. When one use the expr command to perform multiplication in an expression, one must use a backslash as an escape character before the multiplication operator, as the * character is recognized by the shell as a special character. This is as shown below. The construct for expr is $((...))
let command is equivalent to enclosing an arithmetic expression in double parentheses. The construct for let is ((...))
This is equivalent of the following command but displays files in form of branches of a tree:
It is used for creating hard links and symlinks (soft links) in linux.
Note: Symlinks are file-system objects used for creating symbolic links which are just pointers pointing to a file or directory, where as Hard links creates a direct association with the target file by acting as its mirror (because a hard link creates an alias, or an associated name with the same inode number)
head lets user view only the top 10 lines of data in a file. one can also pass the argument n to mention how much lines should the head command display. tail is the exact opposite which displays the last 10 lines of a file.
wc or word count simply tells the number of lines, word count, byte and characters count
This is used to compare two files.
which is a tool for locating the path to a command while whereis also does the same but also returns the location of manpage
locate and find - both are used to search a file by its name. The key difference is that locate uses a previously built database while file searches in the filesystem. While locate is faster, find is more aggressive with options such as -name, -type and -exec
It is used to know more about the type of file to the user. please note that file commands reports on the basis of the contents inside the file and not based on file extension.
This command is used to find other commands based on the keyword that has been passed as an argument
top is similar to task manager of windows i.e., to see the running processes. htop unlike top is interactive process viewer with functionality such as searching, tree view and capability to use a mouse with it
To list running services, one can use the ss command
lsof command stands for List Open Files. Since Linux sees every object as a file, such as devices, directories, etc., unidentified open files prevent users from modifying them.
It is stream editor. It brings the contents of a file into a stream, where modification operations such as text substitution or other text manipulation operations like insert, delete, search, and more. Please note that it doesn't change the actual file but in the stream. To change in the actual file, one can use re-director (>) to ultimately save the content in new file.
Usually there are two out streams: one is main o/p and one is error. When only > is used, only o/p is redirected to a file or process but when both needs to be redirected, then 2>&1 has to be used
gpg (GNU Privacy Guard) is used for creating encrypted keys and actually encrypting a file. As part of encryption for data at rest, gpgtar is used for encrypting files or directories using tar for compression of the said file.
dd is also known as disk/data duplicator. As per manpage, dd can be explained as follows:
du is used to check the space of an entire directory w.r.t to what size a particular file or subdirectory has taken while df is used to check the overall disk space.
Also known as terminal multiplexer. This is particularly used to maintain terminal continuity across sessions. So, let's just say, one person is taking ssh and ran a command which will take, let's just 16 hours and in between, the person closed the terminal, so when he/she comes back, the command that was running needs to be re-run because when the session was closed, the command was also mid-way terminated. To solve, this problem, tmux was brought in. The logic is that once ssh access is taken, one needs to start a tmux session in that terminal and type all commands in that tmux, so even if ssh access is closed, ssh session when regained, can access the tmux to continue the work.
It is the newer alternative for older nslookup.
It has following options:
ssh has a switch option called as -t which can be used to specify the shell one wants while ssh'ing
One can also determine the type of shell using the command:
Everyone knows ssh, but suppose one is accessing a remote server via ssh and the remote server doesn't have GUI, only terminal then the following commands can use tunnelling using port forwarding to access an application running locally on the remote server from host machine.
Here, -N is only for port forwarding and not executing remote commands, -f backgrounds ssh, -o is used for passing configuration values (serveraliveinterval is used to keep the connection alive after every 60 seconds), -L is used to forward a local port to the remote server's port
Note: One can also use -q for quiet mode
For example, to access the python server which is running on localhost of linux server, one can issue the following command: ssh <username>@<ip-address> -N -f -o ServerAliveInterval=60 -L 8000:localhost:8000
Here, -D is used for the remote linux server to act as socks proxy and -C is used for compression to save bandwidth.
The operation can be easily understood as shown below:
Now, let's just assume that there is a remote server, where one can ssh into from local machine, but from that remote server, if one wants to access a webserver hosted on local machine, then reverse port forwarding can be quite helpful.
Let's take the following example:
On local machine, a webserver is hosted on port 8088
Now, for this webserver to be available to linux server, one can open terminals, one for reverse port forwarding (telling that remote server when access webserver locally on 8088, it will connect with webserver hosted on local machine) and other for interaction as shown below:
Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.
It can be best described as written below from manpages:
One of the most used option is to use socat to connect to two TCP streams on separate streams. As per syntax, socat options <bi-address> <bi-address>
Thus the following command can be used to connect two data streams as shown below:
Note: fork - put every connection into a new process, reuseaddr - keeps the port open after a connection is made, & - backgrounds the application
Note, if one wants to create only listener or only connect, then give the 1st bidirectional address as hypen (-)
As per help section, tr is simply used to convert small-case to upper-case or delete characters
grep has the following switches which are commonly used:
grep's -v is used to grep everything in file except the passed argument:
These are cat-like commands with a scrollbar to avoid displaying the entire file at once. Both the command can be used to read the contents of a text file one page (one screen) at a time. This askubuntu summarizes the commands very easily: