Unix


Linux commands:

File Manipulation & Directory Commands
pwd
To check the present working directory.
ex:pwd
ls
To see content of directory.
ls -l :To see more details including the permissions regarding the contents of directory.
ls -a : to see contents including hidden files of directory.
ls ???* : to see a file whose length is more than 3 characters.
ls –la : To see the file permissions, owners, and sizes of all files, enter:
cat
cat is used to create a file and see the content of file.
cat > filename :to create a file and write content in that.
To append file1 onto the end of file2, enter:
cat file1 >> file2
To view the contents of a file named myfile, enter:
cat myfile
mkdir
Create a directory.
Ex: mkdir directoryname
cd
To change directory.
This command changes the current directory location. By default, the Unix login session begins in the home directory.
(i).To switch to a subdirectory (of the current directory) named myfiles, enter: cd myfiles
(ii).To switch to a directory named /home/wissen/sdm_docs, enter:
cd /home/wissen/sdm_docs
(iii).To move to the parent directory of the current directory, enter: cd ..
(iv).To move to the root directory, enter: cd /
(v).To return to the home directory, enter: cd

rm
This command will remove (destroy) a file. We should enter this command with the -i option, so that we'll be asked to confirm each file deletion.
(i)rm –i filename
(ii).rm –rf
cp
Copy files from one location to another location.
Ex: cp file1 file2
(i).cp –I file1 file2

mv
Move files from source to destination. and for rename also.
Ex: mv file1 file2

sort
Display the content ascending order.
Ex: sort filename.

head
Display first 10 lines in a file
Ex: head filename



tail
Display last 10 lines in a file
Ex: tail filename
find
The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.
Example :To find all of the files named myfile.txt in the current directory and all of its subdirectories, enter:
find . -name myfile.txt -print
To look in the current directory and its subdirectories for all of the files that end in the extension .txt , enter:
find . -name "*.txt" -print
less
This utility displays the contents of a text file one screen at a time, waiting for we to press the Spacebar between screens. This lets we read text without it scrolling quickly off the screen.
Example :To read the contents of a file named textfile in the current directory, enter:
$ less textfile
The less utility is often used for reading the output of other commands.
For example, to read the output of the ls command one screen at a time, enter:
$ ls -la | less
To exit less after viewing the file, press q .
scp
Copy files or directories from one host to another host
rcp
Remote file copy host on network.
gzip
Compress or extract files.
!!
To execute last commands.
nohup
Nohup utility which allows to run command./process or shell script that can continue running in the background after you log out from a shell
nohup command-name &
which
Displays the full path of the file
pscp
This commands is used to copy the files from windows to unix
Syntax: pscp username@hostname:
grep
This is used to search a particular pattern(string) in a file
Ex: grep java filename
Ex: ps -ef | grep java java related processes are displaying

cmp
This command is used to compare the two files byte by byte.
Ex:cmp file1 file2
We can compare directories also by using cmpdir.
Ex:dircmp -r /home/test1 /home/test2
diff
This command is used to compare the two files line by line.
Ex: diff file1 file2
sdiff
Side by side compare
vi
This is text editor used to create ,edit a file
$ vi filename
To quit without saving: q!
To save and execute: x (or): wq
0move to the beginning of the line
$-->move to the end of the line
Ctrl-fscroll forward
Ctrl-bscroll backward




chsh
To Change shell
Ex: chsh –s ksh

User & User Group Commands

w
Who logged on system what they are doing.
Id
Display current user and group id names.
Who
Displays current logged in user information
Finger
Display current logged in user with full information(including logged time, name)
Whoami
Displays current user.
Logout
Terminate the session
Users
Print the username of the users currently logged in to the current host

Groups
Print the groups a user is in.

Disk & Memory Commands

mount
Mounting is done with the mount command
$ mount /dev/fd0 /mnt/floppy
unmount
Unmounting is done with the umount command
umount /mnt/floppy
chmod
To give file permission
Ex: chmod 777 filename
du
Display disk used spaces
df
Display disk free spaces
free
It displays status of memories.

Sar(System Activity Report)
Displays the swap, disk space, I/O, system and cpu status.
Troubleshooting Commands

ps
Displays Running process with PID
• ps - a:
• ps -u:
• ps –ef:
Wall
Displays the contents of a file or standard input to all logged-in users.
Hostname
Displays host name.
telnet
Communicating with other host. Uses the telnet protocol to connect to another remote computer.
Syntax: telnet [ host [ port ] ]
Ex:telnet ipaddress/hostname
History
Displays the list what are the commands you are executed. List of all the commands that you have entered. Each command is given a number according to the order it was entered.
Locate
find files by name
Kill
Terminates a process.
 kill -9
 Kill -3 this is for thread dump
Ping
Sends ICMP ECHO_REQUEST packets to network hosts.
Ex:ping
Script
Used to record interactive session.
Ex: script –a –f filename
netstat
Displays network information
env
Displays the environment variable
echo
Displays the variable in the standard out put
Ex: $echo $PATH  Displays Path value
export
Used to set environment variable
Ex: export PATH=
Pidof
Pidof finds the process id (pids) of the named programs.
Fuser
Fuser Displays the pids of processes using the specified files or file systems
Ex: fuser
Fuser 7001/tcp it will display process id associated with port 7001



FAQs (?) :-
What tools or commands are recommended to troubleshoot performance related problems?

A: Following are the basic or essential tools that every Linux or UNIX system admin should be aware of:
a) top
b) sar
c) vmstat
d) iostat
e) free

Command to display the Linux processes in a hierarchical format i.e. Parent and child format
• Process list based on process id -- pstree
• Process list based on username -- pstree

How to browse through directory structure
• tree -d will display only the directories. i.e Files will not be displayed.
• tree -a will display hidden files along with directories and files.
• tree -s will display the file size. While using this option, it prints out the size of the files along with the file names.
• tree -p will display the permissions along with the files.

How can we open files having different file formats?
By using “less” we can see the contents of a file
• less filename.pdf
• less filename.doc
• less filename.tar
• less filename.zip
• less filename.iso
• less filename.rpm

How to Find Files Based on Access / Modification / Change Time
You can find files based on following three file time attribute.

Access time of the file. Access time gets updated when the file accessed.
Modification time of the file. Modification time gets updated when the file content modified.
Change time of the file. Change time gets updated when the inode data changes.
min argument treats its argument as minutes.
time argument treats its argument as 24 hours.
Example 1: Find files whose content got updated within last 1 hour
Following example will find files in the current directory and sub-directories, whose content got updated within last 1 hour (60 minutes)
find . -mmin -60
In the same way, following example finds all the files (under root file system /) that got updated within the last 24 hours (1 day).
find / -mtime -1
Example 2: Find files which got accessed before 1 hour
To find the files based up on the file access time, the option -amin, and -atime is used.
find -amin -60
find / -atime -1

Example 3: Find files which got changed exactly before 1 hour
To find the files based up on the file inode change time, the option -cmin, and -ctime is used.
find . -cmin -60
find / -ctime -1
Example 4: Restricting the find output only to files. (Display only files as find command results)
The following find command displays files that are accessed in the last 30 minutes.
find /etc/sysconfig -amin -30
[Note: The above output contains both files and directories]
find /etc/sysconfig -amin -30 -type f
How Do I Make Backups?

A: You can back up a directory hierarchy or complete file system to any media using GNU tar or cpio, the standard *nix tools for this purpose. tar seems to be the more commonly used program currently, and includes command line options to make compressed, incremental, and multi-volume backups.
How Do I Add Temporary Swap Space?

A: In addition to a swap partition, Linux can also use a swap file. Some programs, like g++, can use huge amounts of virtual memory, requiring the temporary creation of extra space. To install an extra 64 MB of swap space, for example, use the following shell commands:
# dd if=/dev/zero of=/swap bs=1024 count=65535
# mkswap /swap
# swapon /swap
The count= argument to dd determines how big the swap file will be. In this example the swap file's name is /swap, but the name and location are, generally, arbitrary, depending only on the file system's available space and your having write permissions in the directory.
When you don't need the swap space any more, remove it with the following statements:
# swapoff /swap
# rm /swap

What Is a core File?
A: A core file is created when a program terminates unexpectedly, due to a bug, or a violation of the operating system's or hardware's protection mechanisms. The operating system kills the program and creates a core file that programmers can use to figure out what went wrong. It contains a detailed description of the state that the program was in when it died.
If would like to determine what program a core file came from, use the file command, like this:
$ file core
That will tell you the name of the program that produced the core dump. You may want to write the maintainer(s) of the program, telling them that their program dumped core.
[Eric Hanchrow]
How To Enable or Disable Core Dumps?
A: By using the ulimit command in bash, the limit command in tcsh, or the rlimit command in ksh. See the appropriate manual page for details.
This setting affects all programs run from the shell (directly or indirectly), not the whole system.
If you wish to enable or disable core dumping for all processes by default, you can change the default setting in linux/sched.h. Refer to definition of INIT_TASK, and look also in linux/resource.h.
How do I find out Linux CPU Utilization?
A: $ top
How do I find out Linux CPU Utilization individually?
A:$ mpstat
How can I find out Linux Resource utilization using vmstat command?
vmstat command reports information about processes, memory, paging, block IO, traps, and cpu activity
$ vmstat -S M
How to watch the log file in real time?
A: tail –f

How to Find Out Virtual Memory PAGESIZE?
A: $ getconf PAGESIZE or getconf PAGE_SIZE
How much RAM does my Linux desktop PC have?
A: $ free –m
OR
$ free –mt
How to find process id of running program?
A: Pidof finds the process id (pids) of the named programs (Make sure you can see PID of your service)
$ pidof
How can we resolve hostname?
A: # dig your-domain.com
# nslookup gw.isp.com
How to find out the total amount of free and used physical and swap memory in the system?
A: $free
What is the command to see whether the host is alive or dead?
A: $ ping
How to get Information about Active / Inactive Memory Pages?
A: $ vmstat –a
What is the command to know how long the system is up and running?
A: $ uptime
How to find out process memory usage?
A: $ pmap –d PID
How to check out how much hard drive space is left?
A: $ df –h

How to list all the files that are currently open on system?
A: $ lsof

What is the command to find where the binary is stored and its library?
A: $ whereis

How to get the Command line parameters of a process?
ps -fp
cat /proc//cmdline
How to know which process is using a particular port?
Netstat –ap | grep 7001

Command to display the Process hierarchy?
$ ps -e -o pid, args --forest

How to take thread dump?
Find java process
• Ps –ef | grep java
Take thread dump
• Kill -3

Check connectivity between two systems?
Ping
Print list of file/files are consuming more disk space?
Find . –type f –exec ls –sh {} \; | sort –n –r

How do I find out the cpu architecture information under Linux OS?
A: $ proc/cpuinfo
Or
$ less proc/cpuinfo
Or
$ lscpu  It lists cpu architecture information
How to display currently running process?
$ ps -ef
Where
• -e to display all the processes.
• -f to display full format listing
How to List the Process based on the UID?
$ ps -f -u wwwrun,postfix