Linux provides a rich set of command-line utilities that simplify file management, data manipulation, and task automation. This blog post explores key Linux commands, complete with usage examples and exercises to help you sharpen your skills. Let’s dive in!
Finding Files with the find
Command
find
CommandThe find
utility is an incredibly powerful tool for locating files and directories based on various criteria. Here are some examples:
Find all .txt
files in the current directory:
$ find . -name “*.txt” |
Search for files (case-insensitive) beginning with “capital” in the specified directory:
$ find /home/$USER/LinuxClass -iname “capital*” |
Locate files modified exactly 40 minutes ago, less than 40 minutes ago, or more than 40 minutes ago:
$ find /home/$USER/LinuxClass -type f -mmin 40 |
$ find /home/$USER/LinuxClass -type f -mmin -40 |
$ find /home/$USER/LinuxClass -type f -mmin +40 |
Delete .bak
files:
$ find /home/$USER/LinuxClass -name “*.bak” -delete |
Execute a command for every .txt
file found (e.g., listing details):
$ find . -name “*.txt” -exec ls -la {} \; |
Sorting and Removing Duplicates with uniq
uniq
The uniq
command is handy for processing sorted files to remove or identify duplicate lines. It works best when combined with sort
:
Display unique lines:
$ uniq bears |
Show only duplicate lines:
$ uniq -d bears |
Count occurrences of each unique line:
$ uniq -c bears |
The Power of sort
sort
The sort
command allows you to organize file content:
Sort alphabetically:
$ sort grades.txt |
Sort in reverse order:
$ sort -r grades.txt |
Sort by the second field:
$ sort -k2 grades.txt |
Combine multiple flags for precise sorting:
$ sort -bnr -k2 grades.txt |
Using Pipes for Efficient Command Chaining
Pipes (|
) allow you to direct the output of one command as input to another:
Combine, sort, and remove duplicates from two files:
$ cat college1 college2 | sort | uniq |
Count unique items and redirect to a file:
$ cat college1 college2 | sort | uniq > Colleges |
Exercises: Practice Your Skills
Exercise 1: Using find
, locate the file named colors
and all files containing “bear” (case-insensitive). Also, find files modified in the last 45 minutes. How many did you find?
Exercise 2: Combine grocery1
and grocery2
files using cat
, sort and deduplicate them, and save the results in grocery3
. Count the unique items and identify how many start with the letter “c”.
More Essential Commands
history
: Displays command history.
$ history |
alias
: Create shortcuts for commands.
$ alias hist=”history 20″ |
cut
: Extract specific fields from files.
$ cut -f1,4 -d: famousdogs |
diff
: Compare two files.
$ diff numbers1 numbers2 |
date
** and ****cal
**: Display current date/time or calendar.
$
date $ cal |
Input, Output, and Error Management
Linux commands can handle input (STDIN), output (STDOUT), and errors (STDERR). Here are examples of managing them
Redirect errors to a file:
$ grep dog dogs* 2> errors |
Redirect both output and errors to a single file:
$ grep dog dogs* > out_plus_errs 2>&1 |
Advanced Tools: awk
and sed
awk
and sed
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
awk
: A text-processing powerhouse. Rearrange words in a file:
$ awk ‘{print $1,$5,$3,$4,$2,$6}’ hare_tortoise |
sed
: A stream editor for search and replace. Replace all occurrences of “beat” with “defeated”:
$ sed ‘s/beat/defeated/g’ hare_tortoise |
Character Translation with tr
tr
Translate or substitute characters:
Convert text to uppercase:
$ echo ‘Let’s Go Caps!!!’ | tr “a-z” “A-Z” |
Replace newline characters with spaces:
$ tr “\n” ” ” < numbers2 |
Conclusion
Mastering these Linux commands can significantly boost your productivity and command-line expertise. Practice the exercises, experiment with different options, and discover the full potential of Linux utilities!
Leave a Reply