It is often said that there is never any justification for things being complex when they could be simple. Most of the time we spend a lot of effort trying to a complete a task which in fact can be accomplished by a single stroke in the command line. For instance, to remove all the empty files in a graphical environment, you have to go through the search options, select the files and finally delete them. Whilst if you working on a terminal the same task can be done via a single command.

Generally when we talk about command line, we start relating command-line stuff with geek stuffs which is not true. All I am saying is that, it is worth trying to learn a few commands if it saves your time and reduces complexities.

So, if you go through this article, you will eventually appreciate the wonders of the shell commands.

1. Extract specific field from a colon delimited file and save it to another file

# cut -d: -f 2,5 sourcefile.txt > targetfile.txt

This is really useful when the source file has hundreds of lines.

2. Sort a file in ascending order removing duplicate entries and save the result to a file

# sort -u -o target.txt sourcefile.txt

3. Sort specific field from a colon delimited file removing duplicate entries and save the result to a file

# sort -t: -u -k 2 -o target.txt sourcefile.txt ( to sort in descending order use -r option )

4. Display the largest file (in size) of the current directory.

# du -s * | sort -nr | head -1

or

# ls -al | sort -nr -k5 | head -1

( to display top 5 largest files in size, use: head -5 )

5. Combine two files and save the result in a file

# join file1.txt file2.txt > target.txt

6. Remove all HTML tags from a file

# awk ‘{gsub(“<[^>]*>”, “”)}1′ filename

7. Display the difference between two files

# diff -w file1.txt file2.txt

8. Change the contents of a file to all UPPER-CASE

# tr ‘[:lower:]‘ ‘[:upper:]‘ < test.txt > result.txt

Similarly, to all lower-case

# tr ‘[:upper:]‘ ‘[:lower:]‘ < test.txt > result.txt

9. Remove one or more new lines with a single new line and save the result to a file

# tr -s ‘\n’ < test.txt  > result.txt

10. Remove one or more blank space with a single space and save the result to a file

# tr -s ‘[:space:]‘ ‘\ ‘ < test > result.txt

11. Remove all non-printable characters from a file

# tr -cd “[:print:]” < test

12. Create a list of words from a file

# tr -cs “[:alpha:]” “\n” < test > targetfile

13. Delete all the empty files from your home folder

# find /home -empty -exec rm {} \;

14. Pick a random line from a file

# shuf -n1 filename.txt

15. find all the archive files and extract them

# find /home -type f -name “*.tar.gz” -print | xargs tar -xvzf

16. find all the pdf files and archive it

# find /home -type f -name “*.doc” -print | xargs tar -cvzf /tmp/documents.tar.gz

17. find files that are not modified in last x time

# find /home -type f -name “aaa” -mtime -10

Here if we want files to be more than 100 Mb, we use -size +100M

# find /home -type f -name “aaa” -size +100M -mtime -10

18. find files that were last accessed 2 to 10 minutes ago

# find /home -atime +2 -atime -10

19. find files newer than a specified file

# find -newer mydoc.doc

20. find files that was accessed x days after its status was last changed

# find -used 2

21. find files owned by

a. user

# find /home -user rabi

to find files that do not belong to any user

# find /home -nouser

b. group

# find /home -group fortystones

to find files that do not belong to any group

# find /home -nogroup

22. To download all files of a particular format, like all pdfs

# wget -r -l1 -A.pdf –no-parent http://url-to-webpage-with-pdfs/

( -A option to accept the specified format ie .pdf)

23. Download an entire website

# wget -r http://site-name.com

24. Download a given HTML page with inlined images, sounds, and referenced stylesheets to properly display the HTML page.

# wget -p http://<site>/1.html

25. List all the URLs in a file and download the contents of the URLs with a single command

# wget -i files_with_URLs.txt

Related Linux Articles:

40 Basic Linux Command-line Tips and Tricks

40 Must Know Linux related Terminologies

40 Linux Shell Commands for Beginners

Subscribe to fortystones.
Follow @fortystones on Twitter.
Get updated from our Facebook Fanpage.

Share