10 Lesser Known Commands for Linux


Linux is a popular operating system that is widely used for its flexibility, stability, and security. It is also known for its command-line interface, which provides users with a powerful way to interact with the system. While many Linux commands are well-known and widely used, there are some lesser-known commands that can be very useful for certain tasks. In this article, we will explore some of these lesser-known Linux commands.

xargs

The xargs command is used to execute commands with arguments that are read from standard input. This command can be particularly useful when you need to perform a command on a large number of files or directories. For example, let's say you have a directory with thousands of files that need to be moved to a different location. Instead of typing out the command for each file, you can use the xargs command to do it in one go. Here's an example −

$ find . -type f -name '*.txt' | xargs -I{} mv {} /tmp

In this example, the find command is used to search for all files with the extension '.txt' in the current directory and its subdirectories. The output of this command is then piped to xargs, which reads each file name from standard input and passes it as an argument to the mv command. The -I{} option tells xargs to replace the string '{}' with the file name.

tee

The tee command is used to redirect output to both a file and the standard output (stdout) stream. This command can be particularly useful when you want to save the output of a command to a file and also see it on your terminal screen. Here's an example −

$ ls | tee output.txt

In this example, the ls command lists all the files and directories in the current directory and its subdirectories. The output of this command is then piped to tee, which saves it to the file output.txt and also displays it on the terminal screen.

nl

The nl command is used to add line numbers to a file. This command can be particularly useful when you want to reference specific lines in a large file. Here's an example −

$ nl myfile.txt

In this example, the nl command adds line numbers to the file myfile.txt and displays the output on the terminal screen.

watch

The watch command is used to run a command repeatedly at a specified interval. This command can be particularly useful when you want to monitor the output of a command in real-time. Here's an example −

$ watch -n 1 'ls -l'

In this example, the watch command runs the ls -l command every second and displays the output on the terminal screen.

sort

The sort command is used to sort the lines of a file in a specified order. This command can be particularly useful when you want to sort the contents of a large file. Here's an example −

$ sort -n myfile.txt

In this example, the sort command sorts the lines of the file myfile.txt in numerical order and displays the output on the terminal screen.

split

The split command is used to split a file into smaller pieces. This command can be particularly useful when you need to transfer large files over a network that has a limited file size limit. Here's an example −

$ split -b 1m bigfile.txt smallfile

In this example, the split command splits the file bigfile.txt into smaller files that are 1 MB each. The resulting files are named smallfileaa, smallfileab, smallfileac, etc.

du

The du command is used to display the disk usage of files and directories. This command can be particularly useful when you want to find out which files or directories are taking up the most space on your hard drive. Here's an example −

$ du -sh *

In this example, the du command shows the disk usage of all the files and directories in the current directory. The -s option tells du to display only the total size of each file or directory, and the -h option tells it to display the output in human-readable format (e.g. "1.2G" instead of "1234567890").

comm

The comm command is used to compare two sorted files line by line. This command can be particularly useful when you want to find out which lines are common between two files or which lines are unique to each file. Here's an example −

$ comm file1.txt file2.txt

In this example, the comm command compares the files file1.txt and file2.txt and displays three columns of output: lines that are unique to file1.txt, lines that are unique to file2.txt, and lines that are common to both files.

df

The df command is used to display the amount of free and used disk space on your file systems. This command can be particularly useful when you want to find out how much space is available on your hard drive or other storage devices. Here's an example −

$ df -h

In this example, the df command displays the disk usage and available space for each file system on your computer. The -h option tells df to display the output in human-readable format.

rev

The rev command is used to reverse the order of characters in each line of a file. This command can be particularly useful when you want to reverse the contents of a file. Here's an example −

$ rev myfile.txt

In this example, the rev command reverses the order of characters in each line of the file myfile.txt and displays the output on the terminal screen.

timeout

The timeout command is used to execute a command with a time limit. This command can be particularly useful when you want to set a time limit for a command that might otherwise run indefinitely. Here's an example −

$ timeout 10s sleep 20s

In this example, the timeout command runs the sleep command for 20 seconds but terminates it after 10 seconds because of the time limit set by the timeout command.

tac

The tac command is used to display the contents of a file in reverse order, with the last line first and the first line last. This command can be particularly useful when you want to view a file in reverse order or check the contents of a log file. Here's an example −

$ tac access.log

In this example, the tac command displays the contents of the file access.log in reverse order.

yes

The yes command is used to repeatedly output a string or a character. This command can be particularly useful when you want to simulate user input or automate a process that requires a lot of repetition. Here's an example −

$ yes "yes" | rm -i *

In this example, the yes command outputs "yes" repeatedly, which is piped to the rm command with the -i option to confirm the deletion of each file in the current directory.

pgrep

The pgrep command is used to find the process ID (PID) of a running process based on its name. This command can be particularly useful when you want to find the PID of a process that you need to terminate or monitor. Here's an example −

$ pgrep firefox

In this example, the pgrep command finds the PID of the Firefox web browser process.

Conclusion

Linux is a powerful operating system that provides users with a vast array of commands to interact with the system. While many Linux commands are well-known and widely used, there are some lesser-known commands that can be very useful for certain tasks. In this article, we have explored some of these lesser-known Linux commands, including xargs, tee, nl, watch, sort, split, du, comm, df, and rev. By learning and using these commands, you can become more efficient and effective when working with Linux.

Updated on: 27-Apr-2023

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements