Linux Commands - Quick Reference


Change Directory/List directories

Change directory
cd   ~(home directory)
       /(root directory) 

List files/directories
ls    -l (long format) 
      -a (hidden files) S(Sort by size)
      -R (List sub-directories recursively) 

Create/copy/move File 

Finds the type of the file. useful when the type of file is unknown.
file   <file name>  

Make directory
mkdir   -p <Dir./Sub Dir.> (create parent dirs if not present)
            -m mode or permissions to the directory
eg: mkdir -m 777 newdir

Remove directory(ies)
rmdir   -p <Dir./Sub Dir.> (remove the whole directory structure)
            -v (verbose)

Remove file(s)
rm    -r (Remove directories and their contents)
        -v (verbose)
        -i (interactive)

Copy/Move files
cp (or) mv   -i (interactive)
                    -R (Recursive copying)
                   -v (verbose)
cp   <options> <sources seperated by space> <destination>

Secure copy
scp   -r recursive copy
         -P port 
 -i location of the public key file {for establishing connection with private public key}
scp   <file name1> <file name2> <host name@IP address:dest folder>
eg:  scp test.txt localhost@10.0.2.15:/home/naresh [local to remote copy]
       scp localhost@10.0.2.15:/home/naresh/test.txt /home/ [Remote to local copy]

Symbolic links
ln -s symbolic link
ln <options> <target> <link name>
eg: ln -s /home/test.sh test

Change permissions of a file
chmod -rw-rw-r--x [<user in rwx> <group in rwx> <others in rwx>] where r is read, w is write, x is execute

The pieces of a file mode


eg:  chmod 0+wx file.txt (adds wx to other users)

       chmod g-w file.txt (Removes write permission for group)
       chmod ug=rwx (assigns rwx to both owner and group)
       chmod a-w (removes write permission for all the users) 
       chmod +x (adds execute permission for all the users)
eg in Octal representation:
       r w x in binary is 4 2 1  equivalent to 4+2+1=7
       r - x in binary is 4 0 1  equivalent to 4+0+1=5
       r - - in binary is 4 0 0  equivalent to 4

Reading file contents

Concatenate files and print on the standard o/p
cat   -b add line nos to nonblank lines
        -n add line nos
        -s squeeze blank lines
        -E displays $ at the end of the line
cat   <file name1> <file name2> [ctrl+d for exit]

File Traversal less
less   <space> page by page
         <b> backwards page by page
         <G> EOF
         <g> Top of the file
         </<search text>> search text from top to bottom in the file (n for next, N for previous)
         <?><search text>> search text from bottom to top in the file

Log file/continous file traversal
head,tail   -n <no of line> no. of lines to show
                -f  follow
                 +n start from line 'n'  [used with tail]
head  <options> <file name> <file name>    
head: First part of the file -10 lines , tail: Last part of the file -10 lines

Word count
wc   -l no. of lines
       -c no. of chars
       -w no. of words
       -L length of the longest line
wc   <file name>
Eg: wc test.java
Output  : 9 13 110 Test.java 
Here 9 is no. of lines, 13 is no. of words, 110 is no. of chars

List/Find Processes

Display Linux processes
top   -s <no.> no. of seconds to refresh
        -i toggles idle process
        -k process to kill
        -q exit

Reports a snapshot of current processes
ps    -a all process irrespective of the user 
       -x all running processes
       -u <user name> filters processes by user
       -w shows full command name of the processes
       -C <process name> filters process by process name

Finding the processid
pidof   <process name> 

Sending singnal to a process
kill   -STOP {freezes the process}
        -CONT {Resumes the stopped process}
kill   <flags> <pid>

Execute a process(script/command) periodically
watch   -n <no.> interval in seconds
watch  <command> 

Command help/Locate command

Locate a command
which   <command name> 
eg: which bash 
output : /bin/bash

Short desciption of the command
whatis   <command>

Command manual
man   -k search by keyword
man  <options> <command name>

Add (or) Remove user/Add (or) Remove group/Adding users to group

Create a new user or updated default new user information
useradd -m creates home directory for the user  
              -s default shell; allows the user to use shell
              -g group name
              -c comments
useradd <user name> <options>

Deletes a user account and related files
userdel   -r removes user directory also
userdel   <username>  

Create a group
groupadd   <group name>

Delete a group 
groupdel   <group name>

Linking user to a group
gpasswd   -a adding the user to the group
-d removing the user from the group
gpasswd    <options> <user name> <group name>

Find Memory Available/Memory used 

Disk space available
df    -h human readable form 

Disk space used
du   -h human readable form 
       -s summary

Display the total amount of free and used memory in the system
free   -h human readable form 
         -m megabyte format

Search Files/Search text

File Search
find   -name name of the file
          -mtime <no.> files which are created 'n' days ago
          -P Never follow periodic link  
          -empty shows empty files
          -type type of the file, file or directory 
          -delete delete the files that are found
find   <directory> -name <file name>

Text Search
grep   -i case insensitive
          -n prints the line number
          -v invert match (works opposite to match)
grep   <options> <"search key"> <file name1> <file name2> ..

Translate or delete characters
tr   -d  delete chars
     -c complement
     -s  squeeze
tr  <options>  <SET1> <SET2>
Translates set1 of characters to set2 of characters

eg:   echo hello world | tr a-z A-z
        HELLO WORLD
        echo hello world | tr '[:lower]' '[:upper]'
        HELLO WORLD 
        echo hello world | tr '[:space]'  \t
         HELLO      WORLD 
        echo hello world | tr -d 'h'
          ello world 
        echo "my username is 432234" | tr -cd [:digit:]
          432234  [delete all the characters except numbers]

Network details

Configure a network interface
ifconfig    down {turns off the internet connection}
                up {connects to the internet connection}
ifconfig   <name of new interface> <options>

Print n/w connections, routing tables, interface statistics,etc.,
netstat   -a all
-t shows tcp conections
-u shows udp connections
-l listening state connections
-s statistics
-p pid of the listed conections
-n ports of the listed connections
-c continous status / refreshes the screen periodically
-ie same as ifconfig

Utilities

TAR archiving utility
tar   -c creating an archive
       -x extracting an archive
       -v verbose
       -f  Allows to specify a file name
       -z used for gzip format
tar <options> <tar file name in .tar> <folder to archive>
eg:  tar  -cvf for creating
       tar  -xvf for extracting
       tar  -czvf for creating {Order of options matter}
gz
unzip   

APT package handling utility
apt-get   install  {installs the software}
remove   {removes the software from the system}
remove --purge {removes the software as well as configuration files related to the s/w}
autoremove {removes the supporting software }
update {updates the software}
sudo apt-get install <software name>

Display/echoes the command arguments
echo   -e Removes escape characters while echoing  

update the time stamp of a given file or create an empty file
touch  <file>   

Rich text editor
nano   <file>

Notepad type of editor in linux
gedit   <file>

Date/Calendar 

cal   <year> shows the calendar of all the months
        <month> <year> shows the calendar for a month in the year
date  -s set the date  
date +%d-%h-%y - shows date in dd-MMM-yy format


Mail

mailx 
mutt

Shell utilities

cat > log.txt  - Fills the result of a command to txt
    >> log.txt - Appends the content to the log.txt

wildcards - */ - for directories /*.* - every file /*.log - only files with log extension


Executes multiple commands sequentially

ls; echo hello  

Executes multiple commands sequentially; When the previous command fails, the next command won't be executed and outputs nothing

lS && echo hello

Executes multiple commands sequentially; If the first command is successful, next command won't be run and outputs first command (works like a or statement in any programming language)

ls || echo hello 

Outputting previous command's output to net

ls | grep 'Music' 
Outputs the ls command output to grep, so that grep can use that output for searching



Comments

Popular posts from this blog

Distributed database design using CAP theorem

LDAP - Basics

Easy approach to work with files in Java - Java NIO(New input output)