We use several commands on different unix-like operating systems but there are some lesser known hidden gems that can make their work easier and faster.
This article will introduce you to some hidden gems or useful Linux commands that can help you perform various tasks more efficiently and effectively which scripting or casually administrating a system.
Shell built-ins
A shell built-in (command) is a command that is part of the shell, i.e. bash, zsh, etc., itself, rather than a separate executable file. They are faster as they don’t require creating a new process and loading a binary file.
You can use a shell built-in or type
command to check if a command is a built-in or an external one as follows:
$ type cd
cd is a shell builtin
$ type nano
ls is /usr/bin/nano
$ type ls
ls is aliased to `ls --color=auto'
As you can see, cd
is a built-in command, while nano
is an external one located in /usr/bin/nano
.
Useful shell built-in commands
There are many shell built-in commands available in Bash, the most common Linux shell.
You can use the help
command to see a list of all of them and their descriptions.
Here are some of the most useful ones that you should know:
1. export
The export
command sets or displays environment variables, used to store configuration settings or preferences for programs. Learn more at the export command tutorial.
You can set an environment variable with the syntax export name=value
and display all environment variables using export
without any arguments.
export LANG=en_US.UTF-8
export
declare -x HOME="/home/user"
declare -x LANG="en_US.UTF-8"
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/home/user/.local/bin"
...
We can see all environment variables set including LANG
which we just updated.
2. alias
The alias
command creates or displays aliases for commands. Aliases are shortcuts or alternative names for commands that can save you typing time or make commands more memorable.
To create an alias, you need use the syntax alias name='command'
.
alias la='ls -al'
Now we can use la
instead of typing ls -al
. You can display all aliases with 'alias
' command.
unalias
The unalias
command removes aliases for commands as shown below:
unalias ll
This removes the ll
alias that we created earlier.
3. source
The source
command executes commands from a file in the current shell. Here is a complete tutorial on the source command.
source ~/.bashrc
This executes the commands in the .bashrc
file, which is a common file that stores settings and preferences for the Bash shell.
4. test
The test
command evaluates a conditional expression and returns a true or false value. The expression can be a comparison of strings, numbers, or files, or a logical combination of other expressions. Learn more at how to check if file exists tutorial.
The test
command is often used in conjunction with if
, while
, or case
statements to control the flow of a script as shown in the code snippet given below:
$ test 1 -eq 1
$ echo $?
0
$ test 1 -eq 2
$ echo $?
1
It returns 0 (means true) if the expression is true, and 1 (means false) if the expression is false.
Useful hidden commands in Linux and Unix
There are many less known or less frequently used commands, but can be very helpful for certain tasks.
Here is a list of such commands along with their descriptions and example usage:
Command |
Description |
Example |
[ |
Alias for the test command, which evaluates conditional expressions. |
[ -f file.txt ] checks if regular file file.txt exists |
. |
Shell build-in which executes commands from a file in the current shell environment. |
. script.sh runs the commands in script.sh without creating a new subshell. |
xargs |
Reads input from standard input and executes another command with the input as arguments. |
find / -name "*.txt" | xargs rm deletes all the text files in the system. |
tee |
Reads input from standard input and writes it to both standard output and one or more files. (learn more) |
ls | tee -a list.txt lists the files in the current directory and appends the output to list.txt . |
nl |
Numbers the lines of a file. |
nl one.txt |
watch |
Executes another command periodically and displays the output. (more examples of watch command) |
watch -n 5 tail error.log shows the content of log file every 5 seconds. |
sort |
Sorts the lines of a file or input to organize your data or files in a desired order. (See sort command tutorial) |
sort names.txt sorts the lines of names.txt alphabetically. |
du |
Display disk usage of files and directories. (learn more) |
du -sh * shows the size of each file and directory in the current directory in human-readable format. |
comm |
Compare two sorted files (sort ) line by line and outputs three columns: lines only in the first file, lines only in the second file, and lines common to both files. |
comm file1.txt file2.txt |
df |
Display the disk space usage of your file system. (article) |
df -h shows the available and used space on mounted partitions in human-readable (-h ) format. |
wc |
Counts the number of lines, words, and bytes in a file or input. (learn more from wc command tutorial) |
wc file.txt |
Some of the complex commands to learn happen to be the hardware ones.
Here are most useful hardware related commands to remeber as a system administrator:
Command |
Description |
dmesg |
Shows bootup messages from kernel. |
cat /proc/cpuinfo |
Displays CPU information of the system. (Learn more) |
free -h |
Displays free and used memory. |
lshw |
Lists hardware configuration information. (More info here) |
lsblk |
Displays information about block devices. (See related) |
lspci -tv |
Shows PCI devices in your system in a tree-like diagram. |
lsusb -tv |
Displays USB devices in your system in a tree-like diagram. |
dmidecode |
Shows hardware information from the BIOS. (forum thread) |
hdparm -i /dev/disk |
Displays disk data information of a specified disk device. (badram) |
hdparm -tT /dev/[device] |
Conducts a read-speed test on a specified device or disk. |
badblocks -s /dev/[device] |
Tests for unreadable blocks on a specified device or disk. |
fsck [disk-or-partition-location] |
Runs a disk check on an unmounted disk or partition. (lost+found) |
We hope you found this article helpful and informative to learn some of the most useful shell built-in commands and how to use them. Thank you for reading!