Linux Commands

cd

  • cd : directs to /home

  • cd .. : Goes to parent of current directory

  • cd /folder : using absolute paths, folder start from the root folder /

  • cd /mnt : Mount Drives

  • cd folder : Goes to folder folder in this directory

  • explorer.exe . : opens in windows file explorer (WSL)

ls

  • ls : Lists all folders in this directory

  • ls -al : Gives more information

    You have, from left to right:

    • the file permissions (and if your system supports ACLs, you get an ACL flag as well)
    • the number of links to that file
    • the owner of the file
    • the group of the file
    • the file size in bytes
    • the file modified datetime
    • the file name

    Note

    • This set of data is also generated by the l option.
    • The a option instead also shows the hidden files. Hidden files are files that start with a dot (.)
    • For file permission : r = read , w = write , x = executions access

mkdir

  • mkdir folder : creates folder named abc
  • mkdir folder1 folder2 : creates multiple folders at once
  • mkdir -p folder/nested_folder : creates_ _multiple nested folders by adding the -p option

rm | rmdir

  • rm file : deletes file
  • rmdir folder : _deletes_ folder (The folder you delete must be empty.)
  • rm -rf folder : deletes files and folders and everything included

touch

  • touch abc : creates an empty file (If the file already exists, silently fails)

mv

  • mv abc new_abc : if there is no folder , abc will be renamed to new_abc . Otherwise abc will be moved to that folder
  • mv a1 a2 abc : moves a1, a2 files to folder abc

pwd

  • pwd : prints current path

chmod

  • chmod : changes read write execute permissions (Details)

cat

  • cat in1 or cat < in1 : Reads file
  • cat > in1 : Writes to file and then ctrl+c to exit editing
  • cat >> in1 : Appends to file and then ctrl+c to exit editing
  • cat -n in1 : prints content of in1 with line numbers
  • cat in1 in2 : print contents of multiple files
  • cat < in1 > in2 concatenate contents of single file into new file
  • cat in1 in2 > in3 : concatenate contents of multiple files into new file
  • cat in3 | anothercommand : add another command using |

head | tail

  • head -n file_name : prints first n lines
  • tail -n file_name : prints last n lines

wc

word count

  • wc -l file1 : count lines
  • wc -w file1 : count words
  • wc -c file1 : count characters
  • wc -m file1 : count characters with multibyte support (i.e. emojis count as 1, not as multiple characters)
  • wc -m in* : operate wc -m on all files starting with in

find

  • find . -name '*.txt' : Finds all files with .txt extension
  • find . -name 'in*' : Finds all files and folders starting with in
    • find . -type d -name "in" : search only directory
    • find . -type f -name "in" : search only files
    • find . -type l -name "in" : search only links
    • find . -type f -name 'in*' -mtime +3 : Search files edited more than 3 days ago
    • find . -type f -name 'in*' -mtime -1 : Search files edited in the last 24 hours
    • find . -type f -mtime -1 -delete : delete those who meet the criterias
    • find . -type f -size +100c : search files that have more than 100 characters (bytes) in them
    • find . -type f -size +100k -size -1M : search file size bigger than 100KB but smaller than 1MB
  • find . -type f -exec cat {} \; execute a command on each result of the search . {} is filled with the file name at execution time.