Tuesday, December 22, 2015

Useful Linux Commands

Following are few useful Linux terminal commands:

1. Shows the list of files with each one in new line with modified time (descending) and read write permissions for each file:
  • ls -lrt
  • ls -ltr

2. Shows the list of files containing specific word in them. You can use wildcards i.e. * at the beginning, middle or end:
  • ls -lrt *all* 
  • ls -lrt abc*
  • ls -lrt *.xml

3. Lists files with the word after grep. grep searches for particular word: 
  • ls -lrt | grep "POS"
  • ls -lrt | grep POS
  • ls -lrt <folderName> e.g. ls -lrt ABC <-Shows all the files in ABC folder

4. Search word abc in all files' content:
  • grep abc *

5. Navigates to the logs folder in current directory's parent.
  • cd ../logs

6. Shows the running processes:
  • ps -ef
  • ps -efww

7. Find particular process in current running processes(Following four lines do the same):
  • ps -ef | grep ABC
  • ps -ef | grep "ABC"
  • ps -efww | grep ABC
  • ps -efww | grep "ABC"

8. Commands and description:
  • wc -l : Prints the number of lines in a file.
  • wc -w : prints the number of words in a file.
  • wc -c : Displays the count of bytes in a file.
  • wc -m : prints the count of characters from a file.
  • wc -L : prints only the length of the longest line in a file.

9. Finds count of files within current folder or directories of current folder:
  • find . | wc -l

10. Count number of files containing certain text within current directory:
  • grep -l "ABC" * | wc -l

11. Copy file from one linux host (box) to another host:

  • scp -P 111 abc.jar username@127.0.2.2:/home/dev

12. Checks for running processes that include abc word and dev word in mentioned format: axo user:20,pid,cmd
  • ps axo user:20,pid,cmd | grep dev | grep abc

13. Find 'text' word in files in current directory:
  • grep -r "Text" .
  • grep "Text" *

14. Find text in file in specified directory:
  • grep move test/abc.xml

15. Find text in files in current directory and then within those files, find abc:
  • grep -r "Text" . | grep abc

16. Find particular file in Current directory:
  • find . -name abc.xml

17. Following commands will find specified text in zipped file and view that file:
  • zgrep  "In The File" xyz.zip | less
  • zcat xyz.zip | grep "In The File" | less
  • zcat xyz.zip | grep "In The File"

18. Find the IP Addresses following URL would resolve to:
  • nslookup google.com

19. Show current date and time in current linux box:
  • date

20. Show the history of commands executed on current linux box:
  • history

21. Goes two directories back:
  • cd ../../

22. Given a list of text lines in a file, to display the first 10 lines:
  • head foo.txt

23. To display the last 10 lines:
  • tail foo.txt
  • tail -n3 foo.txt  # the last 3 lines
  • head -n4 foo.txt  # the first 4 lines 

24. Show the latest entries in the specified file, in real-time:
  • tail -f testfile

25. Following commands moves abc.tar file from current directory to /docs :
  • mv abc.tar /docs

26. Following command extracts jar file:
  • jar -xvf abc.jar

27. Following command extracts abc.tar:
  • tar -xvf abc.tar

28. Following has destination folder to extract abc.tar:
  • tar -xvf abc.tar /home/jerry/prog

29. Following command searches ABC word in tarz (zipped folder) files names only, not the content inside files. If u want to grep content, u need to extract the tar ball:
  • tar tvf xyz.tarz | grep ABC

30. Command to show the complete current path:
  • pwd 
31. Following command shows the processes that are using port 9016

  • netstat -tulpna | grep 9016


32. Find text in file located in current directory and sub-directories whose name starts with xyz and ends with xyz
  • grep -r "Text" abc*xyz
33. Sets read write execute permissions for all 3 user in all .sh files

  • chmod 755 *.sh


34. Create Soft link or shortcut to ./2.5 with name abc:

  • ln -s ./2.5 abc


35. Copy directory with files:

  • cp -r dir1/ ~/Pictures/


Please provide your feedback.
Thanks