Top Linux commands that you should know

·

5 min read

Linux commands

I have listed few most frequently used linux commands and it usage, if you have come here for any specific command please use search option.

cd : used for changing directory. cd Desktop it goes to Desktop folder if available.

cd .. : moves to parent directory

ls : it lists all files/folders available in current directory.

ls -a : It lists all files included hidden files

ls -l : It show more info along with file name like size, date modified etc.

ls -R : lists subdirectories recursively.

ls -al : it gives more info for hidden files as well

pwd: it shows present working directory that you are in.

mkdir folder : mkdir is used to create a folder. mkdir folder1 creates folder with name folder1

cat file1.txt : It displays the content in file1.txt onto terminal window.

cat > file2.txt : it creates new file and allows us add content with in terminal.

cp: it is used to copy content of one text file to other cp from_file.txt to_file.text we can use same command to copy directories cp -R source/ source_backup. it copies data source folder to source_backup folder

mv : it is used to move a file to target directory. mv file.txt folder_name. we can also rename files using mv command. mv file1.txt newfile1.txt It actually removes file1.txt and creates newfile1.txt

rm : this is used to remove files. if we need to remove folders and all contents in subfolders recursively we can use it like rm -R foldertodelete. some times we can't delete as folder/file is in open then we can use rm -rf foldertodelete

df: used to display disk free status. df -h : disc free space in human readable format df -m: will give free disc space in mb's

du : it displays disc usage statistics

head : it is used to view first few lines of any file, defaults to 10 lines. head file1.txt tail : it is used to view last few lines of any file, defaults to 10 lines. tail file1.txt and tail -n 5 file1.txt to see last five lines only.

diff : it compares two files line by line. diff file1.txt file2.txt and it output the lines which are no match

locate is used to find files. locate "*.txt" It dispalys all files having .txt as extension

find is used to find files/folders. find folder1 displays all files/folders recursively in folder1. We can use flags/flags to filter just files/folders

find . -type d: to find folders in your current directory. Similarly f flag shows all files

find . -type f -mmin -20 : finds files which are modified less than 20 min ago. find . -type f -mmin +20 : finds files which are modified more than 20 min ago.

find . -type f -maxdepth 1 : to avoid finding recursively find . -type f -size +1K : shows you files which are greater than 1KB chmod u=rwx, g=rx, o=r testfile.txt : this command to used to change permissions. It making user with read, write, execute, and group with read, execute and other with just read.

vi : this command is to edit files. vi tempfile.txt will edit the text file and to save it you need to use esc->:x

grep -w "some word" temp.txt : It searches for complete word match in temp.txt file grep "some word" temp.txt : It searches for word match in temp.txt file, partial match will also be returned

grep -n "word" temp.txt : It searches for word match in temp.txt file and returns line number

grep -B 5 "word" temp.txt : It searches for word match in temp.txt file and returns previous 5 lines including matched line

grep -A 5 "word" temp.txt : It searches for word match in temp.txt file and returns next 5 lines including matched line

grep -i "error" ./*.log : It searches for error in current directory of all files with extension of .log

grep -c "error" ./*.log : It searches for error in current directory of all files with extension of .log and also gives us count that error word repeated in each file

hostname -i: will give you ip address of the host name.

Few Shortcuts

  • Ctrl + A : moves cursor to the beginning
  • Ctrl + E : moves cursor to the end
  • Ctrl + U : remove everything that is available before cursor
  • Ctrl + K : remove everything that is available after cursor
  • in history if you would like to run any command, you can simply execute !10 to run the command available in 10th line of history file. Similarly you can also run previous find command with !find

Miscellaneous

  • cd with no path will move to home directory
  • pipe (|) is actually used to combine multiple commands. input of first will be fed to next
  • man command can be used to get help on any other command. ex: man cd
  • 777 -> represents read, write and execute. these are 3 types of permissions
  • we also have 3 types of users, one own user, another is super user(root) and other users.
  • grep command is case sensitive, but you can make it case insensitive using flag grep -i "san" temp.txt
  • history will actually give all commands that you have used.
  • you can also use regular expression in grep command like grep -p "\w" temp.txt will list you all words in temp.txt
  • we can also use alias if you feel any command looks bigger and wanted to make it shorter
  • You can add multiple commands in same line using a separator ;

  • source: youtube.com/watch?v=iwolPf6kN-k