Skip to content

Instantly share code, notes, and snippets.

@krishnadey30
Created May 6, 2020 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krishnadey30/77a746f6585dfbea730ba1f6384c495d to your computer and use it in GitHub Desktop.
Save krishnadey30/77a746f6585dfbea730ba1f6384c495d to your computer and use it in GitHub Desktop.

Command

  • apropos

    apropos "remove files"
  • whatis

    whatis rm
  • man

    man man

    You can use f or b to move forward or backward and use / can be used to search for specific term and press enter to search for next occurrence.

    /browser

    q can be used to quit.

    man 7 man

    here 7 is the section which you want to look at.

  • info

    info git
  • help

    help if

Files

The Linux File Hierarchy Structure or the Filesystem Hierarchy Standard (FHS) defines the directory structure and directory contents in Unix-like operating systems.

  • ls

    -l: vertical view

    -a: show hidden files as well

    -R: recursively go inside each directory

  • file <filename>

  • stat

  • which tells the location of the program

  • touch creates a file with the given file name

  • pwdprint working directory

  • cd is used to change the directory

  • cp is used to copy. cp source destination. Last argument is the destination, so you can copy more than one file.

  • mv is used to relocate the file/directory. Also used to rename

    mv myfile myfile2 #rename
    mv myfile2 Documents #move
  • rm is to delete a file

  • mkdir creates a directory(Cannot be space separated)

    mkdir my directory #creates two directory
    mkdir "my directory" #created a directory
    mkdir my\ directory2 #creates a directory
  • rmdir removes the directory

  • truncate is used to create files of different sizes

    truncate -s 1MB file1
  • find

    find . -name apple #search if file named apple exists
    find . -name "*apple*" #search for files apple in the file name 
    find . -size -10M #files with size less than 10M
    find . -size +10M #files with size greater than 10M
    find . -name "lemon" -type d #returns only directory

    f for file, l for link, c for character device, bfor block device.

  • echo takes text as input and returns it as output

    echo "krishna"
  • > replaces the contents but >> appends to the file.

  • | chains the commands. send the output of one command as input to other.

    ls | wc
  • << Is for hearing

    wc << over
    > This so something
    > over
  • stderr has code 2

    ls docs 2> errout.txt #if output is there then it saves in errout.txt
    
    find / -name "home" 2> error.txt 1> output.txt
  • diff is to find the difference in two files

    diff -y file1.txt file2.txt
    diff -u file2.txt file2.txt
  • cmp to compare byte by byte

    cmp file1.txt file2.txt #first difference
    cmp -l file1.txt file2.txt #all difference
  • hexdump

glob

  • * zero or more character
  • ? one character

nano

  • ctrl + e end of line
  • ctrl + a start of the line
  • ctrl + j to justify
  • M is esc
  • ^ is shift
  • ctrl + _ to go to a line number
  • ctrl + k to cut
  • ctrl + u to paste

Vim

Two modes: command and insert

Command modes takes key strokes as command and insert mode let you type in a document.

i is used to go to insert mode. esc to go back to command mode.

:w to save

:q to quit

In command mode use / and then text to search.

Use ^ to move to the beginning of a line and $ to move to the end.

Links

  • Hard link and Soft(symbolic) links
  • Hard link point to inode (address).
  • Soft link points to the file name(renaming breaks the soft link)
ln -s /home/krishna/users.txt Documents/user-list.txt

ln is for link, -s is for symbolic and then full absolute path and then destination. Relative path will is not used a system will use the relative path from where the sym link is, so it will point to users.txt in Documents

Hard Link:

ln /home/krishna/users.txt Documents/list.txt

Archives

Archives: combines files into one file

Compression: reduce the size of a file

  • tar: Tape Archive

    Does linear scanning

    tar -cf docs_archive.tar * 

    -c is for create

    -f is to tell that it should redirect the output to a file

    * here tells that archive all the files

    List all the files in a archive

    tar -tf docs_archive.tar

    -t is for tar

    Unpack

    tar -xf docs_archive.tar -C extract_folder

    -x to extract

  • Gzip and Bzip

    tar -czf files.tgz file*.txt text*

    -z is for Gzip

    -j is for Bzip

  • zip

    zip myfiles.zip *
    unzip myfiles.zip -d unzipped #-d to specify a folder

Regex

image-20200505131735167

  • z{2} search for zz

  • \. search for .

  • ^ beginning of line $ end of line

    cat users.txt  | grep -E ".*"
    cat users.txt  | grep -E "a"
    cat users.txt  | grep -E "[Aa]"
    cat users.txt  | grep -E "[A-M][a-g]"
  • sed: stream editor

    cat users.txt | sed 's/e/d/' #s is for substitute (replace the first `e` with `d`)
    
    cat users.txt | sed 's/e/d/g' #`s` is for substitute (replace all `e` with `d`) and `g` is for greedy
    
    cat users.txt | sed 's/[A-Ea-e]/_/g'
    
    cat users.txt | sed 's/\ *//g' #gets the first name
  • awkis for reformatting

    cat users.txt | awk '{print $2}' #print item 2
    cat users.txt | awk '{print $2 ", " $1}' #print item 2 , item 1
    cat users.txt | awk '{print $2 ", " $1}' | sort

SSH

sudo apt install openssh-server
ip a #to check ip address
ssh user@ip

File transfer

  • SFTP: SSH File Transfer Protocol

    sftp user@ip
    get file3 #to download
    put fil3 # to upload
  • SCP: Secure Copy Protocol

    scp user@ip:<location_file> . #download
    scp file4 user@ip:<location>  #upload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment