9 Useful Commands With Linux Bash

Suraj Singh Bisht
3 min readAug 15, 2023

--

My Terminal
  1. less & more

Tired of scrolling through long text files? Use less or more to view large files page by page. Just hit the spacebar to move forward and 'q' to quit.

less path_to_large_file.txt
more path_to_large_file.txt

2. curl & wget

Get web data like a pro with curl or wget. Use curl to fetch a page's content, or wget to download files directly.

curl https://example.com
wget https://example.com/file.zip

3. grep

When you’re on a search-and-find mission within files, grep is your trusty companion. Hunt down text patterns in files with ease. grep also supports regular expression and recursive search.

grep "search_text" file.txt

# grep any specific word from file and count it
grep -o "search_word" file.txt | wc -l

4. cut

Need specific columns from text data? cut is your slicer. Specify delimiters and get just what you need.

cut -d ',' -f 1,3 data.csv

5. uniq

Dealing with duplicates? uniq can spot and eliminate consecutive duplicate lines, helping you keep your data squeaky clean.

uniq sorted_file.txt

6. sort

Speaking of order sort rearrange lines in a file. It's your go-to for alphabetizing or numerical sorting.

sort unsorted_data.txt

# sort names from text and remove duplicates
sort names.txt | uniq

7. tr

Need to transform characters? The tr command's got your back. Whether it's changing lowercase to uppercase or swapping out certain characters, tr does it all.

echo "Hello, World!" | tr '[:lower:]' '[:upper:]'

8. xargs

Ever wanted to apply a command to multiple inputs? xargs lets you do just that. It takes the output from one command and uses it as input for another.

# remove all file with .txt extension
find . -name "*.txt" | xargs rm

# Creating a Directory Structure from a Text File:
cat dirs.txt | xargs mkdir -p

9. wc

cat logs.txt | grep "error" | wc -l

Few Examples

Example 1. Extracting Links from a Webpage

curl -s https://aiborne.tech | grep -o "http[s]\?://[^\"' ]*" | sort | uniq | grep '.png'

Example 2. Generating a Random Password

tr -cd '[:alnum:]' < /dev/urandom | head -c 10; echo

Example 3. Processing JSON Data

Let’s say you have a JSON file containing information about movies, and you want to perform some operation on it.

Assuming your JSON file (movies.json) looks like this:

[
{
"title": "Three IDIOT",
"release_year": 2016
},
{
"title": "BATMAN",
"release_year": 2013
},
{
"title": "The Matrix",
"release_year": 1999
},
{
"title": "Interstellar",
"release_year": 2014
}
]

For example, if we want to fetch all release_year values,

cat movies.json | jq '.[] | .release_year'

We want to extract the titles and release years of movies released before a certain year.

cat movies.json | jq '.[] | select(.release_year < 2000) | .title' | xargs -I{} echo "Released before 2000: {}"

Example 4: Want to fetch any GitHub account repo links

curl -s "https://github.com/surajsinghbisht054?tab=repositories&type=source" |  grep -o "href=[^: >]*" | grep -E '/surajsinghbisht054/[^\"]*' | cut -d "=" -f 2 | cut -d '"' -f 2 | sort | uniq

--

--

No responses yet