Useful Linux Commands — Text and Arguments

John Au-Yeung - Feb 13 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

xargs

The xargs command lets us pass output of a command and use it as an argument to another command.

The general syntax is:

command1 | xargs command2
Enter fullscreen mode Exit fullscreen mode

For instance, we can write:

cat deleteall.txt | xargs rm
Enter fullscreen mode Exit fullscreen mode

to the file paths in deleteall.txt to the rm command to delete all the files listed in deleteall.txt .

With the -p switch, we see a confirmation prompt before the command is run:

cat deleteall.txt | xargs -p rm
Enter fullscreen mode Exit fullscreen mode

We can run multiple commands with the -I switch:

command1 | xargs -I % /bin/bash -c 'command2 %; command3 %'
Enter fullscreen mode Exit fullscreen mode

The commands are the ones after the % .

df

The df command lets us get disk usage information.

We can use the -h switch to get the values in a human-readable format.

nohup

The nohup command lets us run a command that doesn’t end when the terminal is killed.

We just put the command after nohup to run it.

diff

diff lets us compare the content of 2 text files.

For example, we run:

diff dogs.txt dogs2.txt
Enter fullscreen mode Exit fullscreen mode

to compare the content of dogs.txt and dogs2.txt .

The -y switch will make diff compare the 2 files line by line.

We can also use it to compare directories with the -r switch:

diff -r dir1 dir2
Enter fullscreen mode Exit fullscreen mode

We can compare which files differ with the r and q switches together:

diff -rq dir1 dir2
Enter fullscreen mode Exit fullscreen mode

uniq

The uniq command lets us get unique lines of text from a file.

For instance, we run:”

uniq dogs.txt
Enter fullscreen mode Exit fullscreen mode

to get the unique lines of text from the dogs.txt file.

We can also use it to get unique lines of output from a command with the | operator:

ls | uniq
Enter fullscreen mode Exit fullscreen mode

We can sort the lines before getting the unique lines with the sort command:

sort dogs.txt | uniq
Enter fullscreen mode Exit fullscreen mode

We can display only the duplicate lines with the -d switch:

sort dogs.txt | uniq -d
Enter fullscreen mode Exit fullscreen mode

The -u switch makes it only display the unique lines:

sort dogs.txt | uniq -u
Enter fullscreen mode Exit fullscreen mode

The -c switch lets us count the occurrences of each line:

sort dogs.txt | uniq -c
Enter fullscreen mode Exit fullscreen mode

sort

The sort command lets us sort lines of text or text command outputs.

For example, we run:

sort dogs.txt
Enter fullscreen mode Exit fullscreen mode

to sort the lines in dogs.txt .

We can use the -r switch to sort the items in reverse order:

sort -r dogs.txt
Enter fullscreen mode Exit fullscreen mode

And we can use the -u switch to return only the unique lines:

sort -u dogs.txt
Enter fullscreen mode Exit fullscreen mode

We can also use sort to sort command outputs. 

For instance, we can sort the output from the ls command with:

ls | sort
Enter fullscreen mode Exit fullscreen mode

Conclusion

We can pass arguments to commands, and diff and manipulate text outputs with various Linux commands.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .