This is Linux commands question I need a simple example for each of the commands
ID: 3838898 • Letter: T
Question
This is Linux commands question
I need a simple example for each of the commands, really simple but with a use of extensinos so not just " wc filename.txt " but to be " wc -c filename.txt "
Commands
Explanations
mkdir
Makes new directory.
rmdir
Removes directory.
ls
Lists the contents of the directory.
cd
change directory.
date
Displays current date and time.
df
Displays the disk usage on partition.
du
Displays disk usage of director.
uname
Displays name of machine logged into.
who
Displays "who" is logged on the system.
top
Displays cpu processes memory.
ps
Displays current running processes.
man
shows help files.
info
shows help files.
--help
help shows help files.
su
Allow you to login as Super User.
ping
to verify IP addresses.
sudo
giving permissions to users with the sudo command.
grep
search a given string in a file.
find
used to search files.
echo
string variable to standard output.
chmod
change the access permissions to file system objects (files and directories).
od
dumps files in octal and other formats.
expr
evaluates an expression and outputs the corresponding value.
test
used as part of the conditional execution of shell commands.
read
A common use for user-created variables is storing information that a user enters in response to a prompt.
vi
The default editor that comes with the UNIX operating system is called vi (visual editor).
nano
It is a small, free and friendly editor which aims to replace Pico, the default editor included in the non-free Pine package.
wc
I used to provide a total of the number of words there are in a file.
cat
Allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.
more
It lets you view text files or other output in a scrollable manner.
less
used to view files instead of opening the file.
Commands
Explanations
mkdir
Makes new directory.
rmdir
Removes directory.
ls
Lists the contents of the directory.
cd
change directory.
date
Displays current date and time.
df
Displays the disk usage on partition.
du
Displays disk usage of director.
uname
Displays name of machine logged into.
who
Displays "who" is logged on the system.
top
Displays cpu processes memory.
ps
Displays current running processes.
man
shows help files.
info
shows help files.
--help
help shows help files.
su
Allow you to login as Super User.
ping
to verify IP addresses.
sudo
giving permissions to users with the sudo command.
grep
search a given string in a file.
find
used to search files.
echo
string variable to standard output.
chmod
change the access permissions to file system objects (files and directories).
od
dumps files in octal and other formats.
expr
evaluates an expression and outputs the corresponding value.
test
used as part of the conditional execution of shell commands.
read
A common use for user-created variables is storing information that a user enters in response to a prompt.
vi
The default editor that comes with the UNIX operating system is called vi (visual editor).
nano
It is a small, free and friendly editor which aims to replace Pico, the default editor included in the non-free Pine package.
wc
I used to provide a total of the number of words there are in a file.
cat
Allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.
more
It lets you view text files or other output in a scrollable manner.
less
used to view files instead of opening the file.
Explanation / Answer
Every version of UNIX comes with an extensive collection of online help pages called manual pages. These are often referred to as man pages .
$ man ls
Listing Files
To list the files and directories stored in the current directory. Use the following command:
$ ls
Here is the sample output of the above command:
$ ls
bin hosts lib res.03
ch07 hw1 pub test_results
ch07.bak hw2 res.01 users
docs hw3 res.02 work
The command ls supports the -l option which would help you to get more information about the listed files:
$ls -l
total 1962188
drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml
-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
Here is the information about all the listed columns:
1. First Column: represents file type and permission given on the file. Below is the description of all type of files.
2. Second Column: represents the number of memory blocks taken by the file or directory.
3. Third Column: represents owner of the file. This is the Unix user who created this file.
4. Fourth Column: represents group of the owner. Every Unix user would have an associated group.
5. Fifth Column: represents file size in bytes.
6. Sixth Column: represents date and time when this file was created or modified last time.
7. Seventh Column: represents file or directory name.
In the ls -l listing example, every file line began with a d, -, or l. These characters indicate the type of file that's listed.
Prefix
Description
-
Regular file, such as an ASCII text file, binary executable, or hard link.
b
Block special file. Block input/output device file such as a physical hard drive.
c
Character special file. Raw input/output device file such as a physical hard drive
d
Directory file that contains a listing of other files and directories.
l
Symbolic link file. Links on any regular file.
p
Named pipe. A mechanism for interprocess communications
s
Socket used for interprocess communication.
Viewing The Content Of A File
To view the content of a file we use cat command.
$ cat sample.txt
$ cat > sample.txt
$ cat >> sample.txt
Creating Directories:
Directories are created by the following command:
$ mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to create. For example, the command:
$ mkdir mydir
Creates the directory mydir in the current directory. Here is another example:
$mkdir /tmp/test-dir
This command creates the directory test-dir in the /tmp directory. The mkdir command produces no output if it successfully creates the requested directory.
If you give more than one directory on the command line, mkdir creates each of the directories. For example:
$ mkdir docs pub
Creates the directories docs and pub under the current directory.
Creating Parent Directories:
Sometimes when you want to create a directory, its parent directory or directories might not exist. In this case, mkdir issues an error message as follows:
$ mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
In such cases, you can specify the -p option to the mkdir command. It creates all the necessary directories for you. For example:
$ mkdir -p /tmp/amrood/test
Above command creates all the required parent directories.
Removing Directories:
Directories can be deleted using the rmdir command as follows:
$ rmdir dirname
Note: To remove a directory make sure it is empty which means there should not be any file or sub-directory inside this directory.
You can create multiple directories at a time as follows:
$ rmdir dirname1 dirname2 dirname3
Above command removes the directories dirname1, dirname2, and dirname2 if they are empty. The rmdir command produces no output if it is successful.
Changing Directories:
You can use the cd command to do more than change to a home directory: You can use it to change to any directory by specifying a valid absolute or relative path. The syntax is as follows:
$ cd dirname
Here, dirname is the name of the directory that you want to change to. For example, the command:
$ cd /usr/local/bin
Changes to the directory /usr/local/bin. From this directory you can cd to the directory /usr/home/amrood using the following relative path:
$ cd ../../home/amrood
File System and Administration Commands
Disk Usage:
The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine. The du command has many parameter options that can be used to get the results in many formats. The du command also displays the files and directory sizes in a recursively manner.
1. To find out the disk usage summary of a /home/tecmint directory tree and each of its sub directories. Enter the command as:
[root@tecmint]# du /home/tecmint
40 /home/tecmint/downloads
The output of the above command displays the number of disk blocks in the /home/tecmint directory along with its sub-directories.
2. Using “-h” option with “du” command provides results in “Human Readable Format“. Means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.
[root@tecmint]# du -h /home/tecmint
40K /home/tecmint/downloads
4.0K /home/tecmint/.mozilla/plugins
[root@tecmint]# du -h /home/tecmint
40K /home/tecmint/downloads
3. To get the summary of a grand total disk usage size of an directory use the option “-s” as follows.
[root@tecmint]# du -sh /home/tecmint
674M /home/tecmint
4. Using “-a” flag with “du” command displays the disk usage of all the files and directories.
[root@tecmint]# du -a /home/tecmint
4 /home/tecmint/.bash_logout
12 /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24
5. Using “-a” flag along with “-h” displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in Kilobytes, Megabytes etc.
[root@tecmint]# du -ah /home/tecmint
4.0K /home/tecmint/.bash_logout
12K /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24K /home/tecmint/downloads/Phpfiles-org.tar.bz2
40K /home/tecmint/downloads
12K /home/tecmint/uploadprogress-1.0.3.1.tgz
4.0K /home/tecmint/.mozilla/plugins
6. Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks. Use the “-k” (displays size in 1024 bytes units).
[root@tecmint]# du -k /home/tecmint
40 /home/tecmint/downloads
7. To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only. Use the option “-mh” as follows. The “-m” flag counts the blocks in MB units and “-h” stands for human readable format.
[root@tecmint]# du -mh /home/tecmint
40K /home/tecmint/downloads
4.0K /home/tecmint/.mozilla/plugins
8. The “-c” flag provides a grand total usage disk space at the last line. If your directory taken 674MB space, then the last last two line of the output would be.
[root@tecmint]# du -ch /home/tecmint
40K /home/tecmint/downloads
9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag “-–exclude“. See the output there is no txt files entry.
[root@tecmint]# du -ah --exclude="*.txt" /home/tecmint
4.0K /home/tecmint/.bash_logout
10. Display the disk usage based on modification of time, use the flag “–time” as shown below.
[root@tecmint]# du -ha --time /home/tecmint
4.0K 2012-10-12 22:32 /home/tecmint/.bash_logout
2013-01-19 18:52 /home/tecmint
Disk Free Space:
On the internet you will find plenty of tools for checking disk space utilization in Linux. However, Linux has a strong built in utility called ‘df‘. The ‘df‘ command stand for “disk filesystem“, it is used to get full summary of available and used disk space usage of file system on Linux system.
Using ‘-h‘ parameter with (df -h) will shows the file system disk space statistics in “human readable” format, means it gives the details in bytes, mega bytes and gigabyte.
Check File System Disk Space Usage:
The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system.
[root@tecmint ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23185840 51130588 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm
Display Information of all File System Disk Space Usage.
The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.
[root@tecmint ~]# df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23186116 51130312 32% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
Show Disk Space Usage in Human Readable Format
Have you noticed that above commands displays information in bytes, which is not readable yet all, because we are in a habit of reading the sizes in megabytes, gigabytes etc. as it makes very easy to understand and remember.
The df command provides an option to display sizes in Human Readable formats by using ‘-h’ (prints the results in human readable format (e.g., 1K 2M 3G)).
[root@tecmint ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/cciss/c0d0p2 75G 23G 49G 32% /
/dev/cciss/c0d0p5 24G 22G 1.2G 95% /home
/dev/cciss/c0d0p3 29G 25G 2.6G 91% /data
/dev/cciss/c0d0p1 289M 22M 253M 8% /boot
tmpfs 252M 0 252M 0% /dev/shm
Display Information of /home File System
To see the information of only device /home file system in human readable format use the following command.
[root@tecmint ~]# df -hT /home
Filesystem Type Size Used Avail Use% Mounted on
/dev/cciss/c0d0p5 ext3 24G 22G 1.2G 95% /home
Display Information of File System in Bytes
To display all file system information and usage in 1024-byte blocks, use the option ‘-k‘ (e.g. –block-size=1K) as follows.
[root@tecmint ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23187212 51129216 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm
6. Display Information of File System in MB
To display information of all file system usage in MB (Mega Byte) use the option as ‘-m‘.
[root@tecmint ~]# df -m
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 76525 22644 49931 32% /
/dev/cciss/c0d0p5 24217 21752 1215 95% /home
/dev/cciss/c0d0p3 29057 24907 2651 91% /data
/dev/cciss/c0d0p1 289 22 253 8% /boot
tmpfs 252 0 252 0% /dev/shm
7. Display Information of File System in GB
To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.
[root@tecmint ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/cciss/c0d0p2 75G 23G 49G 32% /
/dev/cciss/c0d0p5 24G 22G 1.2G 95% /home
/dev/cciss/c0d0p3 29G 25G 2.6G 91% /data
/dev/cciss/c0d0p1 289M 22M 253M 8% /boot
tmpfs 252M 0 252M 0% /dev/shm
8. Display File System Inodes
Using ‘-i‘ switch will display the information of number of used inodes and their percentage for the file system.
[root@tecmint ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/cciss/c0d0p2 20230848 133143 20097705 1% /
/dev/cciss/c0d0p5 6403712 798613 5605099 13% /home
/dev/cciss/c0d0p3 7685440 1388241 6297199 19% /data
/dev/cciss/c0d0p1 76304 40 76264 1% /boot
tmpfs 64369 1 64368 1% /dev/shm
9. Display File System Type
If you notice all the above commands output, you will see there is no file system type mentioned in the results. To check the file system type of your system use the option ‘T‘. It will display file system type along with other information.
[root@tecmint ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 ext3 78361192 23188812 51127616 32% /
/dev/cciss/c0d0p5 ext3 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 ext3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 ext3 295561 21531 258770 8% /boot
tmpfs tmpfs 257476 0 257476 0% /dev/shm
10. Include Certain File System Type
If you want to display certain file system type use the ‘-t‘ option. For example, the following command will only display ext3 file system.
[root@tecmint ~]# df -t ext3
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23190072 51126356 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
11. Exclude Certain File System Type
If you want to display file system type that doesn’t belongs to ext3 type use the option as ‘-x‘. For example, the following command will only display other file systems types other than ext3.
[root@tecmint ~]# df -x ext3
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 257476 0 257476 0% /dev/shm
12. Display Information of df Command.
Using ‘–help‘ switch will display a list of available option that are used with df command.
[root@tecmint ~]# df –help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.
Mandatory arguments to long options are mandatory for short options too.
-a, --all include dummy file systems
-B, --block-size=SIZE use SIZE-byte blocks
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit
SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
Report bugs to <bug-coreutils@gnu.org>.
Changing File Permissions :
You can change file and directory permissions with the chmod command. The basic syntax is as follows:
chmod expression files
Here, expression is a statement of how to change the permissions. This expression can be of the following types: 1) Symbolic 2) Octal
The symbolic expression method uses letters to alter the permissions, and the octal expression method uses numbers. The numbers in the octal method are base-8 (octal) numbers ranging from 0 to 7.
Table-1: Basic Permissions
Letter Permission Definition
r Read The user can view the contents of the file.
w Write The user can alter the contents of the file.
x Execute The user can run the file, which is likely a program. For directories, the execute permission must be set in order for users to access the directory.
Symbolic Method :
The symbolic expression has the syntax of
(who)(action)(permissions)
Table : who
Letter Represents
u Owner
g Group
o Other
a All
Table : actions
Symbol Represents
+ Adding permissions to the file
- Removing permission from the file
= Explicitly set the file permissions
Table : permissions
Letter Represents
r Read
w Write
x Execute
$ chmod a=r *
$ chmod guo=r *
$ chmod go-w .profile
Octal Method :
By changing permissions with an octal expression, you can only explicitly set file permissions. This method uses a single number to assign the desired permission to each of the three categories of users (owner, group, and other).
The values of the individual permissions are the following:
Read permission has a value of 4
Write permission has a value of 2
Execute permission has a value of 1
$ chmod 444 .profile
Network/Communication Commands
When you work in a distributed environment then you need to communicate with remote users and you also need to access remote Unix machines.
There are several Unix utilities which are especially useful for users computing in a networked, distributed environment.
The ping Utility:
The ping command sends an echo request to a host available on the network. Using this command you can check if your remote host is responding well or not.
The ping command is useful for the following:
Tracking and isolating hardware and software problems.
Determining the status of the network and various foreign hosts.
Testing, measuring, and managing networks.
Syntax:
Following is the simple syntax to use ping command:
$ping hostname or ip-address
Above command would start printing a response after every second. To come out of the command you can terminate it by pressing CNTRL + C keys.
Example:
Following is the example to check the availability of a host available on the network:
$ping google.com
PING google.com (74.125.67.100) 56(84) bytes of data.
64 bytes from 74.125.67.100: icmp_seq=1 ttl=54 time=39.4 ms
64 bytes from 74.125.67.100: icmp_seq=2 ttl=54 time=39.9 ms
64 bytes from 74.125.67.100: icmp_seq=3 ttl=54 time=39.3 ms
64 bytes from 74.125.67.100: icmp_seq=4 ttl=54 time=39.1 ms
64 bytes from 74.125.67.100: icmp_seq=5 ttl=54 time=38.8 ms
--- google.com ping statistics ---
22 packets transmitted, 22 received, 0% packet loss, time 21017ms
rtt min/avg/max/mdev = 38.867/39.334/39.900/0.396 ms
$
If a host does not exist then it would behave something like this:
$ping giiiiiigle.com
ping: unknown host giiiiigle.com
$
Filter Commands
Counting Words in a File
You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. Following is the simple example to see the information about above created file:
$ wc filename
2 19 103 filename
$
Here is the detail of all the four columns:
1. First Column: represents total number of lines in the file.
2. Second Column: represents total number of words in the file.
3. Third Column: represents total number of bytes in the file. This is actual size of the file.
4. Fourth Column: represents file name.
You can give multiple files at a time to get the information about those file. Here is simple syntax:
$ wc filename1 filename2 filename3
You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.
To make a pipe, put a vertical bar (|) on the command line between two commands.
When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output, it is referred to as a filter.
The pg and more Commands:
A long output would normally zip by you on the screen, but if you run text through more or pg as a filter, the display stops after each screenful of text.
Let's assume that you have a long directory listing. To make it easier to read the sorted listing, pipe the output through more as follows:
$ls -l | grep "Aug" | sort +4n | more
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 14827 Aug 9 12:40 ch03
.
.
.
-rw-rw-rw- 1 john doc 16867 Aug 6 15:56 ch05
--More--(74%)
The screen will fill up with one screenful of text consisting of lines sorted by order of file size. At the bottom of the screen is the more prompt where you can type a command to move through the sorted text.
When you're done with this screen, you can use any of the commands listed in the discussion of the more program.
Process/Job Control Commands
ps command:
What does 'ps'mean?
ps is the shortage for Process Status. The command should be used to display the currently running processes on Unix/Linux systems. If you know the 'Task-Manager' which pops up under Windows NT/2000/XP when you press CTRL+ALT+DEL then you have a clue what ps does under Unix/Linux. Ps can show you the running processes on your system in different ways. I will describe the basic ones you should know.
Why is it good to know how the ps command works?
If you have a process which seems to hang (e.g. netscape navigator on some buggy websites) and you want to stop the process, then you can determine the process id of the process. Why do you need the process id? You can stop the process with the help of the 'kill' command. The kill command needs a process number otherwise it won't know to which process it should send the 'kill' signal. Other example. You have started a process and now your machines becomes slower and slower. You stop the process but your machine still gets slower and slower. Now it will be helpful if you can stop the process. In this case you also need its pid.
Next example: You want to know what processes your friend just uses (you know he has an remote session open), then you can also use the ps command to find out which processes belong to him.
When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system.
Whenever you issue a command in UNIX, it creates, or starts, a new process. When you tried out the ls command to list directory contents, you started a process. A process, in simple terms, is an instance of a running program.
The operating system tracks processes through a five digit ID number known as the pid or process ID . Each process in the system has a unique pid.
Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system because it is the pid that UNIX uses to track each process.
Starting a Process:
When you start a process (run a command), there are two ways you can run it:
Foreground Processes
Background Processes
Foreground Processes:
By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen.
You can see this happen with the ls command. If I want to list all the files in my current directory, I can use the following command:
$ls ch*.doc
This would display all the files whose name start with ch and ends with .doc:
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc
The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard.
While a program is running in foreground and taking much time, we cannot run any other commands (start any other processes) because prompt would not be available until program finishes its processing and comes out.
Background Processes:
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.
The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand ( &) at the end of the command.
$ls ch*.doc &
This would also display all the files whose name start with ch and ends with .doc:
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc
Here if the ls command wants any input (which it does not), it goes into a stop state until I move it into the foreground and give it the data from the keyboard.
That first line contains information about the background process - the job number and process ID. You need to know the job number to manipulate it between background and foreground.
If you press the Enter key now, you see the following:
[1] + Done ls ch*.doc &
$
The first line tells you that the ls command background process finishes successfully. The second is a prompt for another command.
Listing Running Processes:
It is easy to see your own processes by running the ps (process status) command as follows:
$ps
PID TTY TIME CMD
18358 ttyp3 00:00:00 sh
18361 ttyp3 00:01:31 abiword
18789 ttyp3 00:00:00 ps
One of the most commonly used flags for ps is the -f ( f for full) option, which provides more information as shown in the following example:
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts/6 0:00 ps -f
Here is the description of all the fileds displayed by ps -f command:
Column
Description
UID
User ID that this process belongs to (the person running it).
PID
Process ID.
PPID
Parent process ID (the ID of the process that started it).
C
CPU utilization of process.
STIME
Process start time.
TTY
Terminal type associated with the process
TIME
CPU time taken by the process.
CMD
The command that started this process.
There are other options which can be used along with ps command:
Option
Description
-a
Shows information about all users
-x
Shows information about processes without terminals.
-u
Shows additional information like -f option.
-e
Display extended information.
Stopping Processes:
Ending a process can be done in several different ways. Often, from a console-based command, sending a CTRL + C keystroke (the default interrupt character) will exit the command. This works when process is running in foreground mode.
If a process is running in background mode then first you would need to get its Job ID using ps command and after that you can use kill command to kill the process as follows:
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts/6 0:00 ps -f
$kill 6738
Terminated
Here kill command would terminate first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows:
$kill -9 6738
Terminated
top Command:
The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.
The top command monitors CPU utilization, process statistics, and memory utilization. The top section contains information related to overall system status - uptime, load average, process counts, CPU status, and utilization statistics for both memory and swap space.
The top command produces a frequently-updated list of processes. By default, the processes are ordered by percentage of CPU usage, with only the "top" CPU consumers shown. The top command shows how much processing power and memory are being used, as well as other information about the running processes.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3166 apache 15 0 29444 6112 1524 S 6.6 2.4 0:00.79 httpd
PID – process ID of the process
USER – User who is running the process
PR – The priority of the process
NI – Nice value of the process (higher value indicates lower priority)
VIRT – The total amount of virtual memory used
RES – Resident task size
SHR – Amount of shared memory used
S – State of the task. Values are S (sleeping), D (uninterruptible sleep), R (running), Z (zombies), or T (stopped or traced)
%CPU – Percentage of CPU used
%MEM – Percentage of Memory used
TIME+ – Total CPU time used
COMMAND – Command issued
Text Manipulation/Searching Commands
The grep Command:
The grep program searches a file or files for lines that have a certain pattern. The syntax is:
$grep pattern file(s)
The name "grep" derives from the ed (a UNIX line editor) command g/re/p which means "globally search for a regular expression and print all lines containing it."
A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching.
The simplest use of grep is to look for a pattern consisting of a single word. It can be used in a pipe so that only those lines of the input files containing a given string are sent to the standard output. If you don't give grep a filename to read, it reads its standard input; that's the way all filter programs work:
$ls -l | grep "Aug"
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
There are various options which you can use along with grep command:
Option
Description
-v
Print all lines that do not match pattern.
-n
Print the matched line and its line number.
-l
Print only the names of files with matching lines (letter "l")
-c
Print only the count of matching lines.
-i
Match either upper- or lowercase.
Next, let's use a regular expression that tells grep to find lines with "carol", followed by zero or more other characters abbreviated in a regular expression as ".*"), then followed by "Aug".
Here we are using -i option to have case insensitive search:
$ls -l | grep -i "carol.*aug"
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
1. Search for the given string in a single file
The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
2. Checking for the given string in multiple files.
Syntax:
grep "string" FILE_PATTERN
This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE
grep
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
First create the following demo_file that will be used in the examples below to demonstrate grep command.
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
1. Search for the given string in a single file
The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
2. Checking for the given string in multiple files.
Syntax:
grep "string" FILE_PATTERN
This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
The find Command
The find command is used to locate files on a Unix or Linux system. find will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all subdirectories too. The syntax looks like this:
find where-to-look criteria what-to-do
All arguments to find are optional, and there are defaults for all parts. (This may depend on which version of find is used. Here we discuss the freely available Gnu version of find, which is the version available on YborStudent.) For example, where-to-look defaults to . (that is, the current working directory), criteria defaults to none (that is, select all files), and what-to-do (known as the find action) defaults to -print (that is, display the names of found files to standard output). Technically, the criteria and actions are all known as find primaries.
For example:
Find
will display the pathnames of all files in the current directory and all subdirectories. The commands
Find . -print
find -print
find .
do the exact same thing. Here's an example find command using a search criterion and the default action:
find / -name foo
This will search the whole system for any files named foo and display their pathnames. Here we are using the criterion -name with the argument foo to tell find to perform a name search for the filename foo. The output might look like this:
/home/wpollock/foo
/home/ua02/foo
/tmp/foo
If find doesn't locate any matching files, it produces no output.
The above example said to search the whole system, by specifying the root directory (“/”) to search. If you don't run this command as root, find will display a error message for each directory on which you don't have read permission. This can be a lot of messages, and the matching files that are found may scroll right off your screen. A good way to deal with this problem is to redirect the error messages so you don't have to see them at all:
find / -name foo 2>/dev/nul
Date
The date command under UNIX displays date and time. You can use the same command set date and time. You must be the super-user (root) to change the date and time on Unix like operating systems. The date command shows the date and time read from the kernel clock.
UNIX Date Command Syntax
The syntax is:
Date
date “+format”
Task: Display Current Date and Time
Type the following command:
date
Sample outputs:
Tue Oct 27 15:35:08 CDT 2009
When executed without arguments, the date command shows the current date and time.
Task: Set The Current Time
To set the current time to 05:30:30, enter:
Date 0530.30
Task: Set Date
Set the date to Oct 25, 12:45 a.m., enter:
Date 10250045
19.3 exit
About exit
Exit the shell.
Syntax
exit
Issuing the exit command at the shell prompt will cause the shell to exit.
In some cases, if you have jobs running in the background, the shell will remind you that they are running and simply return you to the command prompt. In this case, issuing exit again will terminate those jobs and exit the shell.
Common aliases for exit include "bye", "logout", and "lo".
WHO
As a Linux user, sometimes it is required to know some basic information like :
Time of last system boot
List of users logged-in
Current run level etc
Though this type of information can be obtained from various files in the Linux system but there is a command line utility 'who' that does exactly the same for you. In this article, we will discuss the capabilities and features provided by the 'who' command.
The basic syntax of the who command is :
Who [OPTION]...[ FILE | ARG1 ARG2 ]
Examples of 'who' command
1. Get the information on currently logged in users
This is done by simply running the 'who' command (without any options). Consider the following example:
$ who
himanshu tty7 2012-08-07 05:33 (:0)
himanshu pts/0 2012-08-07 06:47(:0.0)
himanshu pts/1 2012-08-07 07:58(:0.0)
2. Get the time of last system boot
The is done using the -b option. Consider the following example:
$Who -b
System boot 2012-08-07 05:32
So we see that the above output gives the exact date and time of last system boot.
3. Get information on system login processes
This is done using the -l option. Consider the following example:
$ who -l
LOGIN tty4 2012-08-07 05:32 1309 id=4
LOGIN tty5 2012-08-07 05:32 1313 id=5
LOGIN tty2 2012-08-07 05:32 1322 id=2
LOGIN tty3 2012-08-07 05:32 1324 id=3
LOGIN tty6 2012-08-07 05:32 1327 id=6
LOGIN tty1 2012-08-07 05:32 1492 id=1
So we see that information related to system login processes was displayed in the output.
4. Get the hostname and user associated with stdin
This is done using the -m option. Consider the following example:
$who -m
himanshu pts1 2012-08-07 07:58 (:0.0)
So we see that the relevant information was produced in the output.
5. Get the current run level
This is done using the -r option. Consider the following example:
$ who -r
run-level 2 2012-08-07 05:32
So we see that the information related to current run level (which is 2) was produced in the output.
19.6 The w Command
w - show who is logged on and what they are doing
SYNOPSIS
w - [-husfV] [user]
DESCRIPTION
w displays information about the users currently on the machine, and their processes. The header shows, in this order, the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.
COMMAND-LINE OPTIONS
-h
Don't print the header.
-u
Ignores the username while figuring out the current process and cpu times. To demonstrate this, do a "su" and do a "w" and a "w -u".
-s
Use the short format. Don't print the login time, JCPU or PCPU times.
19.7 whoami
About whoami
whoami prints the effective user ID.
Syntax
Whoami [OPTION]
This command prints the username associated with the current effective user ID.
Running whoami is the same as running the id command with the options -un.
Options
--help
Display a help message, and exit.
--version
Display version information, and exit.
Examples
Whoami
Displays the name of the user who runs the command.
Man
About man
On Linux and other Unix-like operating systems, man is the interface used to view the system's reference manuals.
Description
man is the system's manual viewer; it can be used to display manual pages, scroll up and down, search for occurrences of specific text, and other useful functions.
Each argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section number, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections, following a pre-defined order and to show only the first page found, even if page exists in several sections.
General Options
-h, --help
Print a help message and exit.
-V, --version
Display version information and exit.
-C file, --config-file=file
Use configuration file file rather than the default of ~/.manpath.
-d, --debug
Print debugging information.
Examples
man man
View the manual page for the man command.
man --nh --nj man
View the manual page for man, with no hyphenated words or justified lines.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.