Skip to main content

Command Palette

Search for a command to run...

๐Ÿ“ˆ Beyond Basics: Using Advanced Linux ๐Ÿง Commands

Part 1

Updated
โ€ข5 min read
๐Ÿ“ˆ Beyond Basics: Using Advanced Linux ๐Ÿง Commands

๐Ÿ‘‰ Quick Intro ๐Ÿ”ฅ

In this blog, I will explain advanced commands related to:-

  1. Searching files

  2. Text editor

  3. File compression and archiving commands

  4. Redirection

  5. Environment variables

๐Ÿ‘‰ Search Files ๐Ÿ”

  • To search files there are two commands find & locate

  • find is most commonly used, because it gives the latest data

  • locate a bit faster than find, because it searches in locate DB only not the entire file system, locate DB has to be synced with the file system after every change to get the latest data. [so it is avoided]

    find <path> -name <file> --> Search file in given path using file name & return path of file, if file exists

    find <path> -name <pattern> --> Search file in given path using pattern & return path of a file, if file exists

    \==> If you see above image, we can search file by extension or starting file name

    find <path> -empty --> Search empty files or directories in the given path

    find <path> -perm XXX --> Search files or directories that match given permission

    find <path> -type <d/f> --> Search files by type files or directories

    NOTE:- You can combine different flags based on requirement

    find <path> <other-flags> -exec <command> {} \; --> Execute some command on the output of the other flags used

๐Ÿ‘‰ Text Editor ๐Ÿ“

  • There are two most used editors vi and sed
  1. Vi editor

    vi file --> Open file in the editor [create a new file if doesn't exist]

    vi -r file --> Open file in read-only mod

    • There are two modes in vi editor

      ๐Ÿ”ด Command mode

    • vi editor is opened in command mode by default

    • This mode allows us to use shortcuts

      dd --> Delete line where cursor is present

      d<n> --> Delete next n+1 lines including line where cursor is present

      u --> undo

      k --> move up by one line

      j --> move down by one line

      l --> move right by one character

      h --> move left by one character

      $ --> positions cursor to end of line

      0 --> positions cursor to beginning of line

      <n>G --> go to nth line

      w --> positions cursor to next word first letter

      b --> positions cursor to previous word first letter

      ( --> positions cursor to start of file

      ) --> positions cursor to end of file

      /word --> search for word [n for next search, N for previous search]

๐Ÿ”ด Insert mode

  • To get into command mode from insert mode press Esc

    :wq --> save and exit file

    :q --> exit file without doing change

    :q! --> exist file without saving changes

    :n --> position cursor to nth line

  1. Stream editor

    It shows updated on terminal only, wont update source file by default

    sed 's/old/new/' <file> --> display output by changing only first occurrence of word in each line

    sed 's/old/new/n' <file> --> display output by changing nth occurrence of word in each line

    sed 's/old/new/g' <file> : display output by changing all occurrences of word in each line

    sed '<n>d' <file> : display output by deleting nth line

    sed '$d' <file> : display output by deleting last line

    sed '<x>,<y>d' <file> : display output by deleting lines between [x,y]

    Note :- all the changes are not saved by default, you need to use -i after sed to save changes

๐Ÿ‘‰ File Compression and Archiving ๐Ÿ—ƒ๏ธ

  • Archiving files is all about combining multiple files and directories into a single file, for easier portability and storage [size doesn't change]

  • Compressing files means it reduces the size of files and directories by removing unnecessary things and bundling in a single file

  • There are two commands that are used most tar and zip to archive and compress files

    ๐ŸšฉFLAGS

    c --> Create a new archive file

    x --> Extract archive file

    v --> Verbose

    f --> Specify file name

    z --> gzip compression [.tar.gz]

    tar cvf <output> <files-to-be-archived> --> Archive files into a single file

    tar xvf <output> <files-to-be-archived> --> Extract files from the archive file

    tar tvf <tar-file> --> View files in tar file with details same as in ls -l

    tar cvfz <output>.tar.gz <files-to-be-compressed> --> Archive and compress files using gzip compression, it will create .tar.gz output compressed file

    tar xvfz <output>.tar.gz --> Extract compressed file using gzip compression

    \==> zip is not an in-built command, need to install it sudo apt-get install zip

    zip <output-file>.zip <files-to-be-compressed> --> Compress multiple files into a single file

    ๐Ÿšฉ FLAGS

    -d --> Remove file from zip file

    -u --> Replace file with updated one in the zip file or add a new file

    -x --> Exclude files that are to be skipped during compression

    -r --> Include all files and sub-directories

    -m --> After zipping, delete the directory or file

    unzip <zip-file> --> Unzip files from .zip file

    unzip -l <zip-file> --> Display files in .zip file

๐Ÿ‘‰ Redirection โ†ช๏ธ

CommandDescription
cmd < fileInput of cmd is taken from file.
cmd > fileStandard output (stdout) of cmd is redirected to file.
cmd 2> fileError output (stderr) of cmd is redirected to file.
cmd 2>&1stderr is redirected to the same place as stdout.
cmd1 <(cmd2)Output of cmd2 is used as the input file for cmd1.
cmd > /dev/nullDiscards the stdout of cmd by sending it to the null device.
cmd &> fileEvery output of cmd is redirected to file.
cmd 1>&2stdout is redirected to the same place as stderr.
cmd >> fileAppends the stdout of cmd to file.

๐Ÿ‘‰ Environment Variables โœ…

  • Environment variables are used to store configuration settings, system information, & other variables that can be accessed by processes and shell scripts.

  • Already existing environment variables can be seen using env or printenv

    export VAR=Value --> Create a new environment variable [will last only till logout]

    echo $VAR --> Print value of environment variable

    unset VAR --> Remove environment variable

    export -p --> show all exported environment variables

    Happy Learning!!! ๐Ÿ‘‹