Linux Commands

  • cd : Change Directory

    • cd itself will default to the home DIR

    • cd - takes you back to the previous directory you were at


  • pwd : print the directory you are in


  • whoami : prints current user


  • wc: get file size info

    • wc -c unix.md : Get number of bytes of file

    • wc -l unix.md : Get number of lines of file


  • diff: Compares two files

    • diff -y <file1> <file2>: Outputs diff of file1 and file2 side-by-side


  • find: Find a file

    • find . -name "*.py": Finds all .py files from current directory and down


  • tree: Shows files in tree display


  • printenv: Print environment variables


  • curl: cURL lets us query URLs from the command line

    • curl www.google.com: Returns HTML/Scripts of google.com

    • option -i gives information header

    • option -d or --data can be used to post data to a url

      • curl -d "first=Bob&last=Ross" http://<url>

    • pass in username and password: curl -u <username>:<password> <url>

    • Download response e.g. picture: curl -o test.jpg <url> -> outputs response to test.jpg


  • grep: Search for word or expression in a file

    • grep "expression" <file>

    • Use -i to search for expression without being case sensitive

    • Can use it with pipe: cat my_file.txt | grep "hello world"

    • Use with extended regualar expressions: grep -E 'pattern1|pattern2' fileName

      • Or regular grep:

        • grep 'pattern1|pattern2' fileName

        • grep -e 'pattern1' -e 'pattern2' fileName

    • Use -c to get a count of number of grep matches

    • Can also search multiple files: grep -c 'warning|error' /var/log/*log -> Searches all log files…

      • Use the -R flag to recursivly search subdirectories too

    • grep -r "pattern" *: This will search recursivly all files in the current dir and below

    • grep -P "(?<=hello)(.*)(?=world)": Use a pearl regex. This returns all expressions found between “hello” and “world”.


  • uname: prints system info


  • ps: “Process Status”, prints info about running processes

    • ps -A or ps -e prints all running processes

    • ps aux shows all running processes in BSD format

    • ps -u <user> allows you to filter by user

    • ps -C process_name searches the PID of a process by name


  • pidof <process_name>: This returns the PID only of the process

    • sudo kill $(pidof <process_name>)


  • chown: Change the owner of a file

    • sudo chown root <my_file>: Example changing owner to root


  • chmod: Changes permissions to a file

    • Permissions are grouped in 3 sections, Owner, Group and Users. - Owner is the owner of the file which can be changed with chown - Users is all the users of the system

    • Setting permissions with numbers: - 4: read permission - 2: write permission - 1: execute permission

    • chmod 764 <file_name>: Sets RWX permission for owner, RW for Group and R only for users.

    • Adding with letters: - chmod +x <file_name>: This will add execute permissions for Owner, Group and Users


  • echo: Prints a string to console/file

    • Use with -e to for formatting characters: echo -e "nThis is a newline"


  • ctrl+R: Reverse i search.

    • Allows you to search through previous commands.

    • Press ctrl+R and start typing the search string. Hit ctrl+R to move to the next matching command.

    • Use the arrow keys if you want to modify the command before running it.