Linux shell/bash commands

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5303
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Here is a list of some common Linux shell/bash commands , for most of the Linux distributions that you will actually need to use for work.
  1. Create short cut to a command using alias

    Code: Select all

    alias Desktop='cd /home/username/Desktop'
    Now, instead of typing cd /home/username/Desktop, you can simply move to the Desktop by typing 'Desktop' in the command prompt

    Code: Select all

    alias list='ls -la'
    This creates an alias called list, which will use the ls command to print a long-style listing of all files in the current directory (the -l gives a long-style list, and the -a shows all files - including hidden files)
  2. Use apt-get (APT:Advanced Package Tool) for Debian and Ubuntu Linux to install packages over internet using ftp or http/https protocols, for upgrading all system packages in a single operation, for removing packages as well and other usage

    Install package:

    Code: Select all

    apt-get install {package-name}
    Install several packages with a single operations

    Code: Select all

    apt-get install {package-name} {package-name} {package-name} ...
    Upgrade all packages at once

    Code: Select all

    apt-get upgrade
    Upgrade a single package

    Code: Select all

    apt-get upgrade {package-name}
    Try

    Code: Select all

    apt-get upgrade {package-name} -u
    Remove package but keep the configuration files

    Code: Select all

    apt-get remove {package-name}
    Remove package and all the configuration files

    Code: Select all

    apt-get --purge remove {package-name}
    Search for a package name if you don't remember/know it but you are aware of some keywords

    Code: Select all

    apt-cache search "Text-to-search"

    Code: Select all

    apt-cache search "web server" | grep https
    Display package dependencies (a list of other packages on which a package depends on)

    Code: Select all

    apt-cache depends {package-name}
    NB: Check "aptitude" - a text-based interface to the Debian GNU/Linux package system and Synaptic Package Manager (GUI fron end for APT).
  3. Spell checking a file interactively with Aspell

    Code: Select all

    aspell check filename
  4. Compress and decompress files using bzip2 command at a high rate

    Compress a single file and remove the original file

    Code: Select all

    bzip2 filename
    Compress multiple files at once, remove originals

    Code: Select all

    bzip2 file1 file2  file3
    Create a compressed copy of a file and keep the original

    Code: Select all

    bzip2 -c file > file.bz
    See more, bzip2
  5. Read, modify, concatenate text files or display the contents of a file using Unix/Linux Cat command (cat comes from the word conCATenate or glue together two or more files)

    Display the contents of the motd (messages of the day) file to your screen

    Code: Select all

    cat /etc/motd
    Cat's output to a device, e.g., a printer or modem

    Code: Select all

    cat /etc/motd > /dev/modem
    Concatenate files into one big file

    Code: Select all

    cat file1 file2 file3 output bigfile
  6. Most popular commands for files, directory and jobs

    Delete empty directory:

    Code: Select all

    rmdir dir
    Remove/delete directory and its contents repeatedly:

    Code: Select all

    rm -r directory
    This would present a prompt for approval to delete each of the files. If you don't want to receive a prompt for each file, use the following command instead:

    Code: Select all

    rm -rf dir
    Remove a number of files that start with "file.234."

    Code: Select all

    rm file.234*
    Rename directory:

    Code: Select all

    mv old_name new_name
    Check human readable size of each file/directory in a directory including the container directory itself:

    Code: Select all

    du -h directory_name
    Move to home directory

    Code: Select all

    cd ~
    Print the current working directory:

    Code: Select all

    pwd
    Submit job by using bash script(bas-script is the name of the script)

    Code: Select all

    sbatch bash-script
    Create a link (-s means soft link):

    Code: Select all

    ln -s target_dir  linkname

    For example:

    Code: Select all

    ln -s  target_directory_name  pipeline
    Remove link:

    Code: Select all

    rm pipeline
    List everything including those in directories:

    Code: Select all

    ls -l
    Open the bash profile/bashrc to set PATH/environment variable (e.g., PYTHON variable):

    Code: Select all

    vim ~/.bash_profile
    or

    Code: Select all

    vim ~/.bashrc
    Rename in a path:

    Code: Select all

    mv ../old_directory/new_directory/* ./
    
    Search for text and replace:

    Code: Select all

    sed -i 's/original/new/g' fname.txt
    • sed = Stream EDitor
    • -i = in-place (i.e. save back to the original file)
    • s = the substitute command
    • original = a regular expression describing the word to replace (or just the word itself)
    • new = the text to replace it with
    • g = global (that's, replace all and not just the first occurrence)
    • fname.txt = the file name
    Cancel submitted job:

    Code: Select all

    scancel JOBID
    where JOBID is the job ID
  7. Open/Create

    Open a directory:

    Code: Select all

    nautilus directory_name
    Open website by using a default browser:

    Code: Select all

    xdg-open url
    For example:

    Code: Select all

    xdg-open https://forums.tssfl.com
    Use specific browser e.g., firefox to open web:

    Code: Select all

    firefox url
    Use any editor, e.g., gedit to create a file and write contents in it (start with the name of editor, followed by filename):

    Code: Select all

    gedit filename
This list will periodically be updated, especially, by adding more commands.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Linux and Unix Based Operating Systems”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests