Useful Linux Commands for Beginners

Useful Linux Commands for Beginners

Useful Linux Commands for Beginners:

1. Command to get the RAM usage details

free -h

(use -h to get the details in MB/GB, else you will get the details in KB, which is quiet difficult to calculate and understand)

To get the RAM usage details in MB,

free -m

in GB,

free -g

2. Command to get the Disk usage details

du -h

3. Command to get the Disk usage of particular directory

sudo du /temp -h --max-depth=2 | sort -k2

here temp is the directory, if you want to see for all the directories then use this command,

du $dir -hk --max-depth=2 | sort -k2

4. Command to get the Disk free details

df -h

5. Command to clear the console

clear

6. How to Sort the processes by memory usage ?

ps aux --sort -rss

7. How to lists the processes running on particular port?

sudo lsof -i :5000

This command will lists down the processes which are running on the port 5000.

8. How to kill a process by process id ?

kill -9 {PROCESS_ID}

This kills the given process id, after this command recheck command 7 whether the process killed or not, because sometime if the parent process is not killed then child processes alone will be killed and same child process reruns in different process.

9. How to lists down the parent processes by name ?

pstree -hp | grep node

This command lists down the parent process id which is running for “node”

10. How to restart linux machine by command ?

sudo shutdown -r now

11. How to display only the last 100 lines of a log file ?

tail -100 czc.log

This command displays only the last 100 lines of the czc.log file.

12. How to search the particular word in the log file and display 100 lines before and 50 lines below to that given word ?

grep --after-context=100 --before-context=50 'Exception ' czc.log

This command searches ‘Exception ‘ in czc.log file and display 100 above to that and 50 below to that.

13. How to check the size of the particular directory ?

sudo du -sh /tmp

14. How to check the size of the particular directory including sub directories ?

sudo du -h /tmp

15. How to display the file which is greater than 100MB size ?

sudo find /var -type f -size +100M

16. How to delete the file which is greater than 100MB ?

Please be careful to run the below command, as it deletes the file from your Linux machine.

sudo find /var -type f -size +100M -delete

17. How to zip/compress the folder in linux ?

sudo tar -zcvf script.tar.gz script/

here script/ => is the folder which has lot of files.

18. How to unzip/decompress the .tar.gz in linux ?

tar -zxvf script.tar.gz

19. How to copy files from server to server ?

scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /MACHINE1-PATH/script.tar.gz MACHINE2:/MACHINE2-PATH

Make sure you have given the credentials of the machine 2 to allow the content to be pasted there.

If this is a AWS machine make sure to give -i “my-docker-pair.pem” file in the command.

20. How to clear the RAM memory cache ?

free -m 

this command will display the buffers and caches along with the total RAM size and available RAM size in MB’s.

sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches' 
sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches' 
sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'

For more details about the commands and explanations of this executed commands refer the below links.

Stackoverflow

21. How to display hidden files in linux ?

ls -ld .?*

22. How to copy folder from one place to another place (recursively) ?

$ cp -R /ng /ng_backup

Leave a Reply