Linux Commands to know

·

3 min read

  1. PWD -> Present working directory

  2. history - check all previous commands executed

  3. Copy files cp seasonal/summer.csv backup/summer.bck ( this copies seasonal/summer.csv file to backup dir and file as summer.bck)

  4. Move Files mv seasonal/spring.csv seasonal/summer.csv backup/ ( moves multiple files to backup dir)

  5. Rename Files mv can also be used to rename files ( this overwrites existing files) mv course.txt old-course.txt

  6. Delete files rm filename

  7. Delete directory rmdir dirname ( directory has to be empty b4 deleting)

  8. Make Directory mkdir dir name

  9. View files cat filename

  10. View Several Files less dir/demo.csv dir/demo2.csv :n (colon and a lower-case 'n') to move to the next file, :p to go back to the previous one, or :q to quit.

  11. display top n head -n 3 seasonal/summer.csv

  12. display bottom n tail -n 5 dir/filename

  13. How can I list everything below a directory? ls -R -F

  14. get help for a command? man command name

  15. select columns from a file cut -f 3-6,8 -d . filename which means "select columns3 through 6 and columns 8, using comma as the separator". cut uses -f (meaning "fields") to specify columns and -d (meaning "delimiter") to specify the separator.

  16. Check history of all commands run on terminal history

  17. select lines containing specific values grep keyword dir/filename grep can search for patterns as well -c: print a count of matching lines rather than the lines themselves -h: do not print the names of files when searching multiple files -i: ignore case (e.g., treat "Regression" and "regression" as matches) -l: print the names of files that contain matches, not the matches -n: print line numbers for matching lines -v: invert the match, i.e., only show lines that don't match

  18. Store a command's output in a new file head -n 5 season/summer.csv > top.csv

  19. Combine commands using | (pipe operator) cut -d , -f 2 seasonal/summer.csv | grep -v Tooth

  20. count the records in a file wc , wc (short for "word count") prints the number of characters, words, and lines in a file. You can make it print only one of these using -c, -w, or -l respectively.

  21. specify many files at once , head -n 3 dir/.csv

  22. sort lines of text sort sort puts data in order. By default it does this in ascending alphabetical order, but the flags -n and -r can be used to sort numerically and reverse the order of its output, while -b tells it to ignore leading blanks and -f tells it to fold case (i.e., be case-insensitive).

  23. remove duplicate lines uniq use uniq -c to display unique lines with a count of how often each occurs rather than using unique

  24. Stop running a program cltrl + c

  25. How to edit a file nano filename Ctrl + K: delete a line. Ctrl + U: un-delete a line. Ctrl + O: save the file ('O' stands for 'output'). You will also need to press Enter to confirm the filename! Ctrl + X: exit the editor.

  26. Save steps of commands to a file history tail -n 3 | history > steps.txt

  27. Save Commands to re-run later put the commands in a file and name it with .sh extension (filename.sh) run it using bash filename.sh