Nitendra Gautam

Important Linux Command Cheat Sheet

Anyone who works as a developer needs basic Linux knowledge .This post incldues a Linux command Cheat Sheet one needs when working with LinuxBelow are some of the basic Linux command one would need i have worked while in a production environment.

Super User Commands

~$su root :To change to root user 
~$su <username> :Changing to another user
~$man ps :Gives many options or manuals of ps command
~$echo "Hello" > <File Name> :Adding Content to a file and replacing the existing content 
~$echo "hello" >> <File Name>  :Append the content in testing File without deleting the content 

Copying and moving of Files and directory

Copying files from source to destination

Once this is completed ,a copy of file will be copied to desitination without deleting the original source.

~$cp <source> <Destination >:
<Source> can be file or a directory 

Moving/Renaming files from source to destination

~$mv <source> <Destination >:Cutting files from source to destination Or renaming a file or directory

Creating Empty file in linux

~$touch file2.txt :Creates the file2.txt and cannot add content to it 

Working with JAR Files

~$jar cvf/-cvf <filename.jar> <Source> :Creates Jar files
cvf -creates verbose files
~$jar xvf <filename>.jar :Extract jar files 

File Viewing commands

~$cat <file name>|grep <Word>   :Search the <Word> from the <File Name>
~$cat|grep read

Displaying file content

cat <Absolute path to file>
cat <relative path to file >
Head and TAil commands{So that all the files will not be loaded in memory}
~$head  -<N> <File NAme> {Shows 2Nlines of file name from Top}
~$tail -n 50 /var/log/messages

Divide a file into finite number of Lines

 ~$head -<number of lines> >> <file_name>
 Eg: ~$ head -100000 2008.csv >> 2008_small.csv

Count words in file

 ~$wc <file name> 

Count number of Lines in file

 ~wc -l <file name>

Reading specific lines of log files by using sed command

~$sed -n 1500,1525p /var/log/messages   :Reads the log files from line 1500-1525 
#Color a keyword while using tail command
~$tail -f <File Name> | grep --color=auto "<KeyWord>"
Eg: tail -f info.log | grep --color=auto "Dummy"
#Color and filter multiple Key Words using tail/grep
~$tail -f <File Name> | grep --color=auto -E "<KeyWord1>|<KeyWord2"
Eg: tail -f info.log | grep --color=auto -E "Dummy|Person"
~$less -page through files
Eg:Less input.txt

Files and System Properties

~$date :displays the date
~$stat <file name>:Displays file properties

~$uname -m ;gives the bit information of Linux(32 or 64)
~$df -h  :Shows File System of Linux
~$du -hs  :Shows disk usage of Linux System

~$file <file name> :Identify file types
~$sort -sorts the contents of the file
         sort <file Name>
~$cut -Cuts through the files
       cut -d"-" -f 1,3 <File name>
        Cuts the file and displays the 1 and 3rd Column
~$quota  -v:To check you current quota and how much of it has been used

Finding/locating files in Linux

~$grep -r :Searches recursively in whole System
~$find :locate files
~$S locate :locates file via index
~$which dash :shows if dash has been installed
~$which :locate commands
~$which ruby :Checks if ruby has been installed
~$whereis :Locate standard commands
Ex-whereis <Source Path>

Manipulating Text Files

~$cut:Extract Columns from file
~$cut -d"," -f 2,3 <File name>
~$paste :Append Columns
~$tr :Translate characters 
~$sort :sorts lines in columns 
~$uniq :Locate identical lines
~$tee :copy stdin to file and stdout simultaneously 
#Converting an input file into uppercase
dd if='<Input File Name' of='<Output File Name>' conv =ucase
~$cat >abc.txt :Creates the file  {Ctrl+d :save and Exit file}
                                {Ctrl +c :Exiting file without saving
~$cat >> <file name> :Creates the file name if not created and allows to add content /append
~$cat >> abc.txt :allows to Append extra credit text to already existed file
~$cat abc.txt :displays the content of file(We can't modify the content of the file but can append the content )

Count distinct column of csv file

#Get the data

#Count unique elements in second column 
cat csv_data.csv | cut -d, -f2 |sort |uniq |wc -l

File Compression

~$gzip : compress files in Gzip format
~$compress :compress files(unix
~$ compress -v <File Name> :Compresses a file
~$zcat <File name> :To Read Compresses File
~$bzip2 : compress files (Bzip2)
~$zip :Compress files (windows zip)
Extracting a File
~$tar -zxvf <filename>.tar.gz

Comparing Filex in Linux

~$diff :Compare files line by line 
~$comm :compare sorted files
comm <file 1> <file 2>
~$cmp :compare files byte by byte
cmp <file 1> <file 2> 
~$md5sum <File Name>:Compute check sums

Scheduling Jobs In Unix

$sleep: wait for sometime
sleep 10 & : Sleeps in the background 
$watch :Run programs at set intervals
$at :Schedule a job
$crontab :schedule repeated jobs
~$jobs :lists the process running 


[1] Linux Commands