Menu Close

Useful Linux Commands — Text and Arguments

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

For instance, we can write:

cat deleteall.txt | xargs rm

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

We can run multiple commands with the -I switch:

command1 | xargs -I % /bin/bash -c 'command2 %; command3 %'

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

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

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

diff -rq dir1 dir2

uniq

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

For instance, we run:”

uniq dogs.txt

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

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

sort dogs.txt | uniq

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

sort dogs.txt | uniq -d

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

sort dogs.txt | uniq -u

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

sort dogs.txt | uniq -c

sort

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

For example, we run:

sort dogs.txt

to sort the lines in dogs.txt .

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

sort -r dogs.txt

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

sort -u dogs.txt

We can also use sort to sort command outputs.

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

ls | sort

Conclusion

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

Posted in Developer Tools