Commands used in Linux

Linux Commands
  • cd is used to change directory
  • ls is used for listing all the files available in a folder
  • startx is used for starting graphical user interface (GUI)
  • init 0 is used for closing the o/s
  • iwconfig is used for checking wireless adapter
  • passwd is used to change the root password
  • service apache2 is used to start apache web server
  • sshd-generate is used to start SSH service (SSH keys need to be generated first)
  • service ssh start is used to satr secure shell server
  • service pure-ftpd start is used to start the FTP server




To Start GUI enviromentstartxReconfiguring the X server package, you can reset (and often fix) Xorg configurations with the following commandroot@bt:~# dpkg-reconfigure xserver-xorg
If you are using Backtrack 5 on x64 with KDE you should try the followingroot@bt:~# rm /root/.kde/cache-*
Sometimes you may need to also remove the cache folders in /var/tmp by issuing the following commandroot@bt:~# rm -rf /var/tmp/kdecache-*
Setting Your IP manually
We will first set up the networking manually. In the following example we will assume the following addresses and their purposeIP Address      -  192.168.1.112/24
Default Gateway -  192.168.1.1
DNS server      -  192.168.1.1
In order to set these up we will run the following commandsroot@bt:~# ifconfig eth0 192.168.1.112/24root@bt:~# route add default gw 192.168.1.1root@bt:~# echo nameserver 192.168.1.1 > /etc/resolv.conf
Changing the Root PasswordAs you know Backtrack comes with a default username and password (root/toor) it is IMPORTANT that we change that root password especially when running services such as SSH. We can change the password by issuing the passwd command:
root@bt:~# passwd Enter new UNIX password: {enter your new password here } Retype new UNIX password: {enter your new password again} passwd: password updated successfully root@bt:~#


Starting ServicesBackTrack has various services such as Apache, SSH, MySQL, VNC, etc. They are all disabled by defaultTo start a service such as SSH, you can use the service init scripts. For example, to start the SSH serviceroot@bt:~# sshd-generate  # Specific to the SSH service - needed to generate SSH keysroot@bt:~# /etc/init.d/ssh startStarting OpenBSD Secure Shell server: sshd.root@bt:~# /etc/init.d/ssh stop Stopping OpenBSD Secure Shell server: sshd.root@bt:~#
When using a ssh server for the first time on Backtrack you will need to generate keysroot@bt:~# sshd-generate

To enable a service at boot time, you can use the update-rc.d command, for example, having SSH start at boot timeroot@bt:~# update-rc.d -f ssh defaults Adding system startup for /etc/init.d/ssh ...  /etc/rc0.d/K20ssh -> ../init.d/ssh  /etc/rc1.d/K20ssh -> ../init.d/ssh  /etc/rc6.d/K20ssh -> ../init.d/ssh  /etc/rc2.d/S20ssh -> ../init.d/ssh  /etc/rc3.d/S20ssh -> ../init.d/ssh  /etc/rc4.d/S20ssh -> ../init.d/ssh  /etc/rc5.d/S20ssh -> ../init.d/sshroot@bt:~#

Getting information

man commandname
display the manual page for a particular command named commandname
man -S sectionnumber commandname
display the manual page under a specific section numbered sectionnumber for the command named commandname. Sometimes the same command will exist in more than one section. man alone will display the lowest-numbered section only. To see other sections, you'll need to use the -s flag. The different sections are:
  • 1 - user commands
  • 2 - system calls
  • 3 - libc calls
  • 4 - devices
  • 5 - file formats and protocols
  • 6 - games
  • 7 - conventions, macro packages, etc.
  • 8 - system administration
man -l commandname
list all the man pages available for commandname
man -l intro
interesting reading-- when you have the time
man -K keyword
searches all man pages for keywords
info
the other major way to find information about commands. Somewhat obscure to navigate, but quite useful. Basically, navigate using tab key to select a node, return key to enter a node, 'l' key to go back, 'q' to quit. Useful information available through info include:
  • libg++
  • iostream
  • gdb
  • make
  • tar
  • gzip
which commandname
find the path to a commandname
whatis commandname
tells you what a command is and does. works sometimes.

File Manipulation

Some file notations
. (current directory)
./myfile.txt (a file in current directory)
./mydirectory/myfile.txt (a file in a subdirectory of current directory)
mydirectory/myfile.txt (same as above, a file in a subdirectory of current directory)
.. (parent directory of current directory)
../otherdirectory (a sibling subdirectory of current directory [go back one level and down another directory])
/ (the ultimate root directory)
~ (my home directory)
~/mydirectory (a subdirectory of my home directory)
~otheruser (another user's home directory)
filename*.txt ("*" is the wildcard character)
cp fromfilename tofilename
copy from fromfilename tofilename
mv fromfilename tofilename
Move from fromfilename to tofilename.
rm
remove a file (DANGER: this is permanent!)
rm -i
asks you before removing a file
rm -R
recursively remove a directory and every file in it (DANGER)
> filename
redirect output to a file named filename. Overwrites that file. Example: ls > directory.txt
>> filename
redirect output to a file named filename. Appends to the end of that file.
< redirect in
redirect input to a program. Example: myprogram < myfile.txt
| mycommand
pipe output so it goes through a certain command. Example: ls -l | less
| grep myword
pipe output through grep. grep searches for "myword" and displays only those lines containing that word. Example (to search for all files containing ".cc": ls -l | grep .cc
find mydirectory -name "nametolookfor"
searches mydirectory and all its subdirectories for a file named "nametolookfor"
find . -name "proj3*"
searches the current directory and all its subdirectories for a file begining with "proj3".

Moving Around

pwd
shows which directory you are currently in
cd newdirectory
changes to a new directory
cd ~
change to your home directory.
cd [tab key]
change to your home directory.
cd -
changes to the last directory you were in.
mkdir directoryname
Make a new directory.

Executing commands (some are tcsh-specific)

^Z (Control-Z)
This suspends a currently running command. You can then use "bg" or "fg" on it.
bg
Sends your suspended command to run in the background.
fg
Resumes execution of your currently suspended command.
fg jobnumber
Resumes execution of your suspended command with job number jobnumber.. Use "sps" or "jobs" to see job numbers.
mycommand &
runs the command named mycommand in the background
!foo
repeats the last command you ran beginning with the word "foo"
jobs
shows background jobs that were spawned from this current shell.
nice +nicelevel commandname
runs a command with a lower priority. Priorities range from 0 to +19, +19 being the lowest priority available. If you are going to run a cpu-intensive command, it is nice to nice it.
renice +nicelevel jobnumber
resets a particular command to a different priority level
ps
shows your current processes attached to this terminal. Use "ps -aux" to see everything.
top
show the processes that are using the most resources
top -Uusername
show a user's processes in order of resource usage.
kill jobnumber
kill off a particular process (BE CAREFUL)
kill -9 jobnumber
kill off a particularly troublesome process if the above doesn't work (BE VERY CAREFUL)

Using Text Files

cat filename
outputs content of a file to the screen
cat filename1 filename2 > filename3
concatenates filename1 and filename2 into a new file named filename3
more filename
displays a file page by page
less filename
does the same thing as more, but a little cooler
head filename
shows the first 10 lines of a file.
head -numberoflines filename
shows the first specified number of lines of a file. Ex. tail -50 filename
tail filename
shows the last 10 lines of a file.
tail -numberoflines filename
shows the last specified number of lines of a file. Ex. tail -50 filename
nl filename
sticks line numbers in front of each line in your file and redirects output to the screen

Using Other Files

file filename
find out what type of file filename is
gzip filename
compress a file
gunzip filename
uncompress a file
tar cvf tarfilename file1 file2 file3 etc.
archive a bunch of files or directories into one tar file.
tar xvf tarfilename
un-archive a tar file into its constituent files
zcat filename
like gunzip, but sends output to stdout.
zcat filename | tar -xvf -
unzips a compressed, tar'ed file, and untars it directly without creating an intermediate file
eject
eject the cd tray
cdplay
play audio CD's in the cdrom drive.
cat .au > /dev/audio
play an au sound file.
mpg123
play MP3 files.
xpdf
view pdf files
gv
view ps files
xdvi
view dvi files
xview
view gif, jpeg, png files

Spying and Surveillance on Other Users

finger
Check to see if someone is logged into your system.
.plan files
If you put text into a file named ".plan" in your home directory, people will see it when they finger you.
whoami
in case you forget.
rwho
who is on
who
who is on
w
who is on, and what they're doing
/scratch/QUOT
this directory has files that show the biggest user of the /scratch directory
talk username
start chat session with another user
ytalk username
start chat session with multiple users

Learning about the Machine

xload
see the system load
quota -v
How much of your disk and file quota you are using up.
df
How much disk space is free on various drives.
du
How much space files are taking up in this current directory.

Miscellaneous Fun

clock
display a clock
ssh
log into a different machine securely. We recommend using "ssh" over telnet and rlogin. May interfere with Java Swing GUI stuff.
set autolist
Auto-suggest commands you type on the command line.
mesg n
Turns messages off so you won't be disturbed.
from
Tells you who your new mail is from.
biff
A text-based program which will show a message when you receive new mail.
xbiff
shows a picture of a mailbox on your screen. The flag goes up and it beeps when you get new mail.
set nobeep = 1
Turns off that annoying beep.
lynx
text-based web browser

Administrative Commands



(most of these commands must be executed as root)

ifconfig
view and play with your ethernet and tcp/ip settings
su username
change your current shell to one owned by username
mount/umount
mount and unmount file systems
control-panel
brings up the Red Hat control panel
shutdown time
shuts down the system at time
route
manually edit routing table (warning: careful)
init #
manually tell init to go to runlevel #
fsck
check the hard disk for errors
hdparm
can greatly increase the performance of your hard disk (careful)
isapnp
utility to help configure Plug'N'Play devices


Linux Commands

Common Commands
  • pwd: print current working directory.
  • cd: changes Directory to dirname cd pathcd ..
  • ls: display information about file type. ls -als -l.
  • file: determine file type. file filename.
  • less: view file contents. less filename
  • cp: copy source file into destination. cp item1 item2.
  • mv: move (Rename) a oldname to newname. mv item1 item2.
  • mkdir: create a new directory dirname. mkdir directory.
  • rm: Remove (Delete) filename. rm item.
  • ln: Create softlink on oldname. ln file linklink -s item link.
  • cat: Concatenate files. cat [file...]cat 
  • sort: sort lines of text.
  • uniq: report or omit repeated lines.
  • wc: print newline, word, and byte counts for each file. wc filename
  • grep: print lines matching a pattern. grep pattern [file...]
  • head: output the first part of a file. head -n number filename
  • tail: output the last part of a file. tail -n number filename
  • tee: read information from standard input and write to standart output and files.
  • type: indicate how a command name is interpreted. type command.
  • which: display which executable program will be executed. which command.
  • man: display a command's manual page. man program
  • apropos: display a list of appropriate commands. appropos term.
  • info: display a commmand's info entry. 
  • whats: display a very brief description of a command. whats term
  • alias: create an alias for a command. alias name='string'
  • echo: display a line of text. 
  • clear: clear the screen. 
  • history: display the history of content list. 
  • id: display user identity.
  • chmod: change a file's mode. chmod xxx filename
  • umask: set the default file permission.
  • su: run as a shell as another user. .su [-][user] 
  • sudo: execute a command as another user. sudo command
  • chown: change a file's owner. chown [owner][:[group]] file...
  • chgrp: change afile's group ownsership.
  • passwd: change a user's password. passed [user].
  • ps: report a snapshot of current process. 
  • top: display tasks.
  • jobs: list active jobs. 
  • bg: place a job in the background. bg %number
  • fg: place a job in the foreground. fg %number
  • kill: send a signal to a process. kill [-signal] PID 
  • killall: kill processes by name. killall [-signal] name
  • shutdown: shut down or reboot the system. 
  • printenv: print part or all of the environment.
  • set: set shell options.
  • export: export environment to subsequently executed programs. 
  • vi: start the vi editor.

<- Commands for Different Users

List Directory Contents

lsls -lls -a 
List Block Devices
lsblklsblk -l
Compute and Check MD5 Message Digest
md5sum filename
Convert and Copy a file 
dd
Unix Name

print detailed information about the machine name, Operating System and Kernel.

unameuname -a , History Record 
prints the history of long list of executed commands in terminal.

history
Make directory 
mkdir directoryname 
touch 

Update the access and modification times of each FILE to the current time. 

touch filename 
Advanced Package Tool 

Apt is an advanced package manager for Debian based system (Ubuntu, Kubuntu, etc.), that automatically and intelligently search, install, update and resolves dependency of packages on Gnu/Linux system from command line. 

apt-get install packageapt-get update 
Tape Archive 
tar -zxvf abc.tar.gz tar cvf archieve_name dirname/tar xvf archive_name.tar
Calendar 
calcal month year
Find

Search for files in the given directory, hierarchically starting at the parent directory and moving to sub-directories. 

find -name *.sh , find -inname *.sh 
grep 

The grep command searches the given file for lines containing a match to the given strings or words. 

grep word file 
whereis 

locate the Binary, Sources and Manual Pages of the command. 

service 
The service command controls the Starting, Stopping or Restarting of a service. This command make it possible to start, restart or stop a service without restarting the system. 

service apach2 start 
df 

Report disk usages of file system. 

df
du 

Estimate file space usage. Output the summary of disk usages by ever file hierarchically. 

du 
cmp

compare two files of any type and writes the results to the standard output. 

cmp file1 file2 
gcc, g++, java 
gcc file.c./a.out 
gcc -o name file.c./name 
g++ file.cpp./a.oout 
g++ -o name file.c./name 
javac file.javajava file 
ipconfig 

configure the kernel-resident network interfaces. 

ipconfig -a 
netstat 

displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc... netstat -a,netstat -atnetstat -s 

nslookup 
A network utility program used to obtain information about Internet servers. 

nslookup servername 
dig 

dig is a tool for querying DNS nameservers for information about host addresses, mail exchanges, nameservers, and related information. 

dig servername 
wall 

wall sends a message to everybody logged in with their mesg permission set to yes. 

wall "message" 
rename

The rename command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions. 

rename [-v][-n][-f] perlexpr [files] 
change the extension of all these files from ".html" to ".php":

rename 's/\.html$/\.php/' *.html

<- More Find

Find files using name 

find -name "file" 
find files using name and ignoring case 
find -iname "file" 
Search specific directory level 
find / -name passwdfind -maxdepth 2 -name passwd,
 find / -maxdepth 3 -name passwd,
find -mindepth 3 -mxmdepth 5 -name passwd
Executing commands on the files after the find command 
find -iname "file" -exec md5sum {} \; 
Inverting the match 
find -not -iname "file" 
Find file based on the file permissions 
find . -perm -g=r -type f -exec ls -l {} \;
find . -perm g=r -type f -exec ls -l {} \; 
Find all empty files 
find ~ -emptyfind . -maxdeapth 1 -empty 
Find the top 5 big files 
find . -type f -exec ls -s {} \; | sort - n -r | head -5
Find the top 5 no empty small files 
find . -not -empty -type f -exec ls - s {} \; | sort - n | head -5 
Find Files based on file-type
find . -type sfind . -type ffind . -type d
find . -type f -name ".*" 
Find files bigger than the given size 
find ~ -size +100Mfind ~ -size 100M 
Remove big archive files 
find / -type f -name *.zip -size +100M -exec rm -i {} \; 
Find files whose content got updated within last 1 hour 
find . -mmin -60find / -mtime -1 
Find files which got accessed before 1 hour 
find -amin -60find / -atime -1 
Find files which got changed exactly before 1 hour 
find . -cmin -60find / -ctime -1 
Restricting the output only to files 
find /etc/sysconfig -amin -30 -type f 
Find files which are modified after modification of a particular file
find -newer file 
Find files which are accessed after modification of a specific file
find -anewer file
Perform any operation on files found from find command 
find <Condition to find files> -exec <Operation> \;

<- More Grep

Search for the given string a single file

grep "pattern" filegrep -i pattern" filegrep -iw "pattern" file 
Checking for the given string in multiple files 
grep "pattern" file*
Match regular expression in files 

?, *, +, {n}, {n,}, {,m}, {n,m} 

Displaying lines before/after/around the match 
grep -A <N> "pattern" filegrep -B <N> "pattern" filegrep -C <N> "pattern" file 
Highlight the search 
export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' 
Search in all files recursively 
grep -r "pattern" * 
Invert match 
grep -v "pattern" file 
display the lines which does not matches all the given pattern 
grep -v -e "pattern" -e "pattern" 

Count the number of matches 

grep -c "pattern" file 
Display only the file names 
grep -l this demo_* 
Show only the matched string 
grep -o "pattern" file
Show the position of match in the line 
grep -o -b "pattern" file 
Show line number 
grep -n "pattern file