Linux Commands – Echo Expansion, Pipelines, Head, Tail, Cat

Echo Expansion

The echo command has lots of different uses, here are some useful file system commands.

List all folders with uppercase names:

echo [[:upper:]]*

List all files and folders in a path pattern:

echo /usr/*/share

List hidden files:

ls -d .[!.]?*

 

You can also use echo as a calculator (please note it only works with whole numbers):

echo $((2 + 2))

Pipelines

Bash is is very powerful as you can pipeline commands together, here are some examples of pipelined commands:

This example will list files in both the /usr and /usr/bin directories and then sort them alphabetically and only show the unique files and folders:

ls -l /usr /usr/bin | sort | uniq

You can also list only duplicate files

ls -l /usr /usr/bin | sort | uniq -d

In this example, we use WC to confirm the number of files and folder in a directory.  Using wc -l we will perform a line count on our directory listing:

ls -l | wc -l

Head and Tail

The head and tail commands allow you to read the top or bottoms of a files:

head ~/.bash_history
tail ~/.bash_history

You can also monitor a file and read the new lines as they are written

tail -f ~/file.txt

Cat

You can use cat to write into a new file:

cat > filename.txt

Then type what you want to go into the file and press Ctrl + D

 

You can use cat to join multiple files together (it does this in numerical order)

cat VMKernal.log* VMKernal.joined.log