0% found this document useful (0 votes)
101 views

Linux Commands

Linux Commands
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Linux Commands

Linux Commands
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#####################Directories########################

man -k (apropos) >> shows a list of man pages matching a string


whatis
>> shows the desription of man page
wheries >> shows the location of man page
man hier >> shows help for the of linux file system FHS (File heirarchy standa
rd)
which
>> shows the full path of shell commands
#####################Directories########################
cd >> previous path
cd ~
>> home directory
cd ..
>> parent directory
cd /home >> relative directory
cd home
>> current directory is assumed
TAB key
>> auto complete
ls -a
>> show hidden files
ls -l
>> lists contents in long-listing format
ls -ltraS
>> show all files+ hidden in long listing format, recursively
by time + Size
ls -lh
>> shows file size in human readable format (MB, GB, KB) with
long listing format
mkdir
>> make directory
mkdir -p dir1/dir2/dir3 >> create directory and sub directories, dir1 must be
existing
rmdir
>> remove directory
rmdir -p
>> remove directories recursively
mv folder
>> renames folder
#######################Files##########################
touch
>> creates new file
touch -t 201607201300 new file >> creates new files with time stamp
file filename >> Utility determines the file type
file -s
>> for special files in /dev & /proc
rm
>> to remove file
rm -i
>> to prevent yourself from accidentally removing files
rm -rf
>> to recursively remove all files with force option
cp -r
>> to recursively copy files under certain directry
cp -i
>> to avoid overwriting while copying
mv file
>> rename file
mv -i file
>> rename file and take permission to avoid overwriting alread
y existing files
rename .conf .backup *.backup >> renames all files ending with .backup to .conf
rename 's/TXT/text/' *
rename -n 's/TXT/txt/g' aTXT.TXT
################Working with file contents####################
head -n file
>> displays first n lines of file, by default n=10
tail -n file
>> displays last n lines of file, by default n=10
tail -f file
>> displays file contents and update the display automati
cally
cat file
>> displays the contens of file
cat > file
>> creates file with input line after enter, ctrl+d to save
tac file
>> displays backwards
cat file1 > file2 >> copy contens of file1 to file2
cat > file <<stop >> insert inputs to file until type "stop": end mark
more
>> displays the contents of file

less
strings

>> displays the contents of file


>> read readable strings

#####################Linux File Tree#######################


##################Commands and Argumens#################
echo string > file
>> type string in a file ignoring spaces
echo "string" >file
>> type string and keeping spaces in files
echo -e string1 \n string2 >> type string1 and string2 in new line
echo -e string1 \t string2 >> type string1 and string2 in new tab
alias rm='rm -i'
>> to set alias to act as one command
alias
>> show current aliases
unalias
>> to remove certain alias
######################Control Operators####################
;
>> Semicolon, is used to separate commands on same line
&
>> will run the command in background and you will get your shell prompt
back and get a message after the command executed
&&
>> Logical AND; the 2nd command will run if only 1st command runs successf
ully
||
>> Logical OR; 2nd command will run if only first commands fails
>> rm file1 && echo It worked! || echo It failed!
#
>> pound sign is used for comments as its ignord by the shell
\
>> escaping special sign, if you want to add special text lik & \ # !
and space, proceed it by \
\
>> end of line backslash, is used to continue in next line
######################Shell Variables ######################
$HOSTNAME, $USER, $UID, $SHELL, and $HOME >> Examples of shell environmental va
riables
$
>> Shell will interpret after it a shell variable and will start looking f
or environmental variables
$VAR
>> Variables should be case sensitive preceeded by $
MyVar=55
>> creates variables
' variables '
>> shell won't interpret it
"variables"
>> shell will interpret the variables
unset variable >> to remove the variable
$PS1
>> variable determines your shell prompt
$PATH
>> variabe determines where the shell looking for commands to
execute unless the command is aliased or builtin
set
>> list all current shell vaiables
env
>> will display list of exported variables
env -i
>> clears the environment subshells
$()
>> interpret embedded shell for variables
paul@deb503:~$ A=shell
paul@deb503:~$ echo $C$B$A $(B=sub;echo $C$B$A; echo $(C=sub;echo $C$B$A))
shell subshell subsubshell
#########################Shell History ######################
!!
history 5
!25
!to
echo $HISTSIZE
HISTSIZE=250
echo $HISTFILE

>> will repeat the last command Bang Bang!!


>> will show last 5 command history
>> will run the command 25 in shell history
>> will run the last command matching this text after !
>> will show history size
>> will change the history size to 250
>> will show history file location /home/user/.ba

sh_history
echo $HISTFILESIZE

>> will show history file size

#########################File Globbing #######################


*
>> interpreted by shell as a sign to generate filenames
matching the comibination
?
>> interpreted by shell as a sign to generate filenames m
atching single character
[ ]
>> interpreted by shell as a sign to generate filenames
matching any characters or numbers between brackets regardless the order, each
pair of brackets is for only one character.
[!]
>> interprets files doesnt match whats inside the bracket
s
\*
>> to prevent the globbing
echo *
>> interpreted as ls
######################### IO Redirection #######################
Image
set -o noclobber
>>prevents files to be erased /overriden while reading
set -C
>>prevents files to be erased /overriden while readin
g
set +o noclobber >>allows files to be erased/overriden while reading
[paul@RHELv4u3 ~]$ cat winter.txt
It is cold today!
[paul@RHELv4u3 ~]$ set -o noclobber
[paul@RHELv4u3 ~]$ echo It is cold today! > winter.txt
-bash: winter.txt: cannot overwrite existing file
[paul@RHELv4u3 ~]$ set +o noclobber
>|
>> overruling noclobber, for files protected by no
clobber
>>
>> append to files
2>
>> redirecting stderr
2>&1
>> redirecting both stderr and stdout
here document
>> append input until certain sequence (Usually EOF)
$ cat > text.txt <<EOF
> one
> two
> EOF
>foo
>> the quickest way to clear a file
>| bar
>> to clear a file if the noclobber is activated
########################### Filters #########################
tee
>> The tee filter puts stdin on stdout and also into a file. So
tee is
almost the same as cat, except that it has two identical outputs.
tac count.txt | tee temp.txt | tac
grep
>> filters lines of text containing certain string
cat tennis.txt | grep Williams
grep Williams tennis.txt
grep
-i
>> filters lines of text ignoring lower/upper case
grep
-v
>> filters lines doesnt matching the string
grep
-A1 >> displays result with one line after
grep
-B1 >> displays result with one line before
grep
-C1 >> displays result with one line after & one line before
cut
>> used to select colomns from files depending on a delimiter
or count of bytes
Example:It uses the colon as a delimiter, and selects fields 1 and 3.
[[paul@RHEL4b pipes]$ cut -d: -f1,3 /etc/passwd | tail -4

Figo:510
Pfaff:511
Harry:516
Hermione:517
tr

>> translates character into another


$ cat tennis.txt | tr 'a-z' 'A-Z'>translates lower case into upper case
$ cat count.txt | tr '\n' ' ' > translates new lines into space

s
tr

-s

>> translates multiple occurancies into one


cat spaces.txt | tr -s ' ' >> suppress all spaces into o

ne >>> very useful


tr
-d
>> delete characters from files
wc
>> -l > lines
-w: words -c: characters
sort
>> sorts lines in files
sort -k3
>> alphabetic sort the lines for colomn 3
sort -n -k3 >> numeric sort of lines for colomn 3
uniq
>> to remove duplicate from sorted list
uniq -c
>> to remove dulictaes from sorted list and count repitions
comm
>> comparing streams in 2 files
od
>> shows the contetrns of file in hexa format
od -b
>> in octal bytes
od -d
>> ASCII or backslashed sharacters
sed
>> Stream editot
>> $ echo level5 level7 | sed 's/level/jump/g'
jump5 jump7
>> cat tennis.txt | sed '/BE/d' >> will delete BE from file
#######################Basic Unix Tools #######################
find .
>> find files in current directory and sudirs
find -name
>> find with name
find -type f
>> find with type f
find -type d
>> find with type d
find . -type f -name "*.bak" >> find files having names ending with .bak
find -newer file1 >> find files newer than file1
find -exec command { } \; >> find /data -name "*.odf" -exec cp {} /backup/ \;
find /etc -type f -name '*.conf' | wc -l> find no. of files matching .conf in /e
tc
locate
>> locate files but sometimes updatedb
date
>> to display the date
cal 2 2012
>> to display the calender
sleep 5
>> sometimes bash needs to wait for certain time in seconds
time command >> to show how long the command took> time bzip2 text.txt
###################Archiving and Compression###################
Compress:
gzip & gunzip
>> used to compress and uncompress files using gzip format
zcat & zmore
>> used to view the contents of gzip format files
bzip2 & bunzip2 >> used to comoress and uncompress files using bzip2 format
bzcat & bzmore
>> used to view the contents of bzip2bz format files
Archive:
tar -c
tar -x

>> create archive


>> extract tar file

compress and archive:


tar -czvf
file.tar.gz >>> creates archive with gzip to untar > tar -xz
vf
tar -cjvf
file.tar.bz >>> creates archive with bzip2q to untar > tar

-xjvf
######################Vi Commadn Editor######################
getting to command mode:
Esc
>> set vi(m) in command mode.
a >>start typing after the current character
A >>start typing at the end of the current line
i >>start typing before the current character
I >>start typing at the start of the current line
o >>start typing on a new line after the current line
O >>start typing on a new line before the current line
Replace and delete
x delete the character below the cursor
X delete the character before the cursor
r replace the character below the cursor
p paste after the cursor (here the last deleted character)
xp switch two characters
Undo and repeat
u undo the last action
. repeat the last action
Cut, copy and paste a line
dd cut the current line
yy (yank yank) copy the current line
p paste after the current line
P paste before the current line
3dd cut three lines
4yy copy four lines
Start and
0 jump to
^ jump to
$ jump to
d0 delete
d$ delete

end of line
start of current line
start of current line
end of current line
until start of line
until end of line

join two lines


J join two lines
yyp duplicate a line
ddp switch two lines
yyp
words
w forward one word
b back one word
3w forward three words
dw delete one word
yw yank (copy) one word
5yb yank five words back
7dw delete seven words
save and exit vi
:w save (write)
:w fname save as fname
:q quit

:wq save and quit


ZZ save and quit
:q! quit (discarding your changes)
:w! save (and write to non-writable file!)
searching
/string forward search for string
ww?string backward search for string
n go to next occurrence of search string
/^string forward search string at beginning of line
/string$ forward search string at end of line
/br[aeio]l search for bral brel bril and brol
/\<he\> search for the word he (and not for here or the)
Replace
:4,8 s/foo/bar/g replace foo with bar on lines 4 to 8
:1,$ s/foo/bar/g replace foo with bar on all lines
Read files and input
:r fname (read) file fname and paste contents
:r !cmd execute cmd and paste its output
Text buffers
"add delete current line and put text in buffer a
"g7yy copy seven lines into buffer g
"ap paste from buffer a
Multiple files
vi file1 file2 file3 start editing three files
:args lists files and marks active file
:n start editing the next file
:e toggle with last edited file
:rew rewind file pointer to first file
Abbreviations
:ab str long string abbreviate str to be 'long string'
:una str un-abbreviate str
#######################Users Management#####################
usermod -aG wheel username
>> add sudo priviliges to certain user

You might also like