You are on page 1of 13

Dhanoop Bhaskar

Character Description
Escape character. If you want to reference a special
\ character, you must “escape” it with a backslash first.
Example: touch /tmp/filename\*
Directory separator, used to separate a string of directory
/ names.
Example: /usr/src/linux
Current directory. Can also “hide” files when it is the
.
first character in a filename.
.. Parent directory
~ User's home directory
Represents 0 or more characters in a filename, or by
itself, all files in a directory.
*
Example: pic*2002 can represent the files pic2002,
picJanuary2002,picFeb292002, etc.
Represents a single character in a filename.
? Example: hello?.txt can represent hello1.txt, helloz.txt,
but not hello22.txt
Can be used to represent a range of values, e.g. [0-9], [A-
Z], etc.
[ ]
Example: hello[0-2].txt represents the names
hello0.txt,hello1.txt, and hello2.txt
“Pipe”. Redirect the output of one command into another
| command.
Example: ls | more
Redirect output of a command into a new file. If the file
> already exists, over-write it.
Example: ls > myfiles.txt
Redirect the output of a command onto the end of an
>> existing file.
Example: echo .Mary 555-1234. >> phonenumbers.txt
Redirect a file as input to a program.
<
Example: more < phonenumbers.txt
Command separator. Allows you to execute multiple commands
; on a single line.
Example: cd /var/log ; less messages
Command separator as above, but only runs the second
command if the first one
&&
finished without errors.
Example: cd /var/logs && less messages
Execute a command in the background, and immediately get
& your shell back.
Example: find / -name core > /tmp/corefiles.txt &

2
The LINUX Directory Layout

Directory Description

The nameless base of the file system. All other


directories, files, drives, and devices are attached to
this root. Commonly (but incorrectly) referred to as
the “slash” or “/” directory. The “/” is just a
directory separator, not a directory itself.
/bin Essential command binaries (programs) are stored here
(bash, ls, mount, tar, etc.)
/boot Static files of the boot loader.

Device files. In Linux, hardware devices are accessed


/dev just like other files, and they are kept under this
directory.
/etc Host-specific system configuration files.

Location of users' personal home directories


/home
(e.g./home/dhanoop).

/lib Essential shared libraries and kernel modules.

Process information pseudo-file system. An interface to


/proc
kernel data structures.

/root The root (super-user) home directory.

/sbin Essential system binaries (fdisk, fsck, init, etc).

Temporary files. All users have permission to place


/tmp
temporary files here.

/usr The base directory for most shareable, read-only data


(programs, libraries, documentation, and much more).
/usr/bin Most user programs are kept here (cc, find, du, etc.).

/usr/include Header files for compiling C programs.

/usr/lib Libraries for most binary programs.

“Locally” installed files. This directory only really


matters in environments where files are stored on the
/usr/local network. Locally-installed files go in
/usr/local/bin, /usr/local/lib, etc.). Also often used
for software packages installed from source, or
software not officially shipped with the distribution.
/usr/sbin Non-vital system binaries (lpd, useradd, etc.)

/usr/share Architecture-independent data (icons, backgrounds,


documentation, terminfo, man pages, etc.).
3
Program source code. E.g. The Linux Kernel, source
/usr/src
RPMs, etc.

/usr/X11R6 The X Window System.

Variable data: mail and printer spools, log files, lock


/var
files, etc.

BASIC LINUX SHELL COMMANDS

To get help on commands


help <command name>
: help cat
<command name> --help
: grep --help
<command name> -h
: chmod -h
whatis <command name>
: whatis grep

To get “man” pages


man <command name>
: man ls
To search for a particular word within a man page
type “/word”.
To quit from a man page, just
type the “Q” key.
If you don’t remember the actual command
man -k <keyword to search>
: man -k permission
You will find a line in the output that looks something
like: chmod (1) - change file access permissions

To get “info” pages


info <command name>

To print working directory


pwd

To change directory
To return to home directory
cd
To change into specified directory name
cd <directory name>
4
To change to home or other path relative to home.
“~” is an alias for your home directory
cd ~
To move up one directory
cd ..
To return to previous directory
cd -

To list all files in the current directory


To list all files in the current directory, in column format
ls
To list the files in the specified directory
ls <directory name>
To list files in “long” format, 1 file per line with additional
information
ls -l
To list all files, including “hidden” files
ls -a
To show the directory's detailed information
ls -ld <directory name>
To list all files whose names begin with the letter “d” in the
/usr/bin directory
ls /usr/bin/d*
To list files based on their size
ls -sh

To find the type of a file


file <file name>

To create a file
cat > filename.txt
//type the contents and press Ctrl+D to exit

To display the contents of a text file


cat <file name>

To view the contents of a file page by page


more <file name>
//traversal only in forward direction
less <file name>
//traversal in both directions

To display first few lines of a file


head <file name>
head -5 <file name>
5
//first 5 lines

To display last few lines of a file


tail <file name>
tail -5 <file name>
//last 5 lines
To display the last few lines of a text file, and then output
appended data as the file grows (very useful for following log
files!)
tail -f /var/log/messages

To copy a file from one location to another


cp [-r] <source file/directory> <destination>
: cp mp3files.txt /tmp

To move a file to a new location


mv <source> <destination>
: mv mp3files.txt /tmp
To delete a file
rm [-r] <filename/directory name>
: rm /tmp/mp3files.txt

To create a directory
mkdir <directory name>
: mkdir dhan
mkdir -p <directory path>
: mkdir -p /home/sng/dhan
To create directories in multiple paths
mkdir -p parent/{child1,child2,child3}
: mkdir -p work/{in,out,pending}/{a,b,c}

To delete a directory
rmdir <directory name>
: rmdir /tmp/myfiles/

To show the full path of shell commands found in your path


which <command name>
: which grep
//output like: /bin/grep

To locate the program, source code, and manual page for a command
whereis <command name>

6
To search for files anywhere on the file system
locate <keyword to search>
//find all files and directories that contain the keyword

To search for files matching certain patterns


find <directory name> <pattern to search>
: find . -name \*mp3
//starts searching in the current directory “.” and all
subdirectories, looking for files with “mp3” at the end of their
names

To list currently running process


ps
To list all process in the system
ps -aux

To show who is logged on and what they are doing


w
who
To view the name of current user
whoami
To view the user details
finger

To view host name


hostname

To set host name


hostname -a <new host name>

To print your user-id and group id's


id

To get report on file system disk space usage


df
In MB
df -h

To view disk usage in a particular directory


du
To view summary
du -s

In MB
du -h

7
To display CPU processes in a full-screen GUI
top
// type “Q” to quit

To display amount of free and used memory in the system


free

To display information about your CPU


cat /proc/cpuinfo

To display lots of information about current memory usage


cat /proc/meminfo

To print system information to the screen


uname -a

To print kernel version


uname -r

To display the size of a file


wc -l
In bytes
wc -m
In char
wc -c
In word count
wc -w

To change permission to a file/directory


chmod [-R] <permissions> <file name/directory name>
: chmod 754 one.txt
: chmod -R 777 /home/songs/
{
read r=4; write w=2; execute x=1
Owner Group Others
rwx r-x r--
7 5 4
}

To change ownership of a file/directory


chown [-R] <user name> <file name/directory name>
: chown dhanoop one.txt
: chown -R dhanoop /home/songs/

8
To search a file for a particular pattern
grep [-R] <word to search> <file name/directory name>
: grep science science.txt
//case-sensitive search
: grep -i science science.txt
//case-insensitive search
: grep -i ‘spinning top’ science.txt
//multiword search
: grep -R passwd /etc/
//search in directory

To clear the screen


clear

To display text on screen


echo “<text to display>”
: echo “Hello”
To print a file or program output
lpr <file name>
: lpr mp3files.txt
<command> | lpr
: ls -la | lpr

To sort a file or program output


sort
: sort mp3files.txt

To switch user
su <user name>
//to switch to a particular user account
su
//to switch to root account
su -
//to switch to root, and log in with root's environment

To give an alias name to a command for ease of use


alias <name>=”<command>”
: alias list=”ls -l”

To display last logins of user


last

9
To display the name of currently working terminal
tty

To block a user
passwd -l <user name>

To unblock a user
passwd -u <user name>

To add a user account


useradd [options] <username>
With specified home directory
useradd -d <home directory> <user name>
With specified shell
useradd -s <shell> <user name>
With specified initial group
useradd -g <initial group> <user name>
With specified expiry date
useradd -e <YYYY-MM-DD> <user name>

To lock/unlock a user account


usermod [-L or -U] <user name>

To delete a user account


userdel [-r] <user name>

To restart the system


shutdown -r now
//without delay

To turn off the system


poweroff

To reboot the system


reboot

To halt the system


halt

To mount a file system or media


mount <source> <destination>
: mount /dev/sda1 /mnt
//mounting first partition of first SATA hard disk
10
: mount /dev/hdb3 /mnt
//mounting third partition of second hard disk

To umount a file system or media


umount <file system/media>
: umount /mnt

To view IP address
ifconfig

To edit IP address temporarily


ifconfig -a etho <ipaddress>

To view partitions and file systems


fdisk -l
//view line by line

To print selected columns in a file


cut -c <column range> <file name>
: cut -c 3-5 sem.txt

To print particular fields


awk ‘{ print $<field number> }’ <file name>
: awk ‘{ print $2 }’ sem.txt
//prints second field
: awk ‘{ print $1 $3 }’ sem.txt
//prints first and third fields

To know the shell


echo $SHELL
//the symbol “$” precedes every user or system variable.
All system variables are BLOCK lettered.

To know the home directory


echo $HOME

To print the calendar


cal
//prints calendar of current month
cal <YYYY>
: cal 2010
//prints calendar of specified year

11
To print the date
date

To temporarily switch to root to execute a single command


sudo <command>
: sudo ifconfig -a eth0 192.168.1.101

To run an executable file


sh <file name>
: sh new\ file
//”\ “ denotes white space
./<file name>
: ./new\ file

To view command history


history

To re execute last command


!!

To execute nth command in history


!n
: !444
//executes 44th command in the history

To re execute last command that starts with specified character


!<char>
: !s
//executes the last command that starts with letter “s”

To repeat the last command changing old character to new character


^old^new
: ^l^m
//let “wc -l” be the last command
now “wc -m” will be executed

Signals to a process
To list all signals
kill -l
To kill a process
kill <process id>

12
To terminate a process
kill -9 <process id>
To normally exit a process
kill -15 <process id>

To login remotely to a system


ssh [<user name>@]<ip address>
: ssh dhanoopbhaskar@192.168.1.101
To execute a command on remote system through remote login
ssh [<user name>@]<ip address> <command>
: ssh dhanoopbhaskar@192.168.1.101 df -h
//executing “df -h” remotely

To copy file from a remote system


scp [-r] <source> <destination>
: scp /home/a.c dhanoopbhaskar@192.168.1.101:/home/

13

You might also like