SlideShare a Scribd company logo
1 of 14
Download to read offline
UNIX – Training
Directories and Files
· File and Directories Creation, modifying and deleting
· Copying & Moving Files and Directories
· Searching Files and Directories – FIND & GREP
· File Permissions
· Working with editors - vi
· Linking files - Soft links and Hard link
· Scheduling Jobs - CRON
· Locating a process and monitoring processes with ps
· Killing processes - KILL
File and Directories Creation, modifying and deleting
Directory layout
Now that you know how to move around directories and get listings, you're ready to look at the directory layout on a typical UNIX
distribution. You can organize a UNIX file system several ways. This tutorial discusses a few root-level directories that are common to most
UNIX-like distributions. There are other important root-level directories, but this is where you'll find yourself operating in most cases:
/home (or /users)
/etc
/bin
/sbin
/usr
/var
/tmp
Now, consider the possibility of having private information in this file that you don't want any other users to read. You probably want to
remove read access for other groups and all other users. You can use chmod to change the permissions. Like many things in UNIX, there are
multiple ways to use chmod; this section focuses on one. The three categories (user, group, and other) are represented by three letters (u, g,
and o). The three types of permissions (read, write, and execute) are also represented by three letters (r, w, and x). To change permissions,
use chmod followed by the letters of the categories you want to change, followed by a plus or a minus (for turn on or turn off), followed by
the letters of the permissions you want to change. After this, write the name of the file (or files) you want to make the change to. This is best
illustrated with an example:
$ chmod og-r example.txt
$ ls –l
You should see this result:
-rw------- 1 tuser admin 0 Aug 13 15:35 example.txt
In this example, you specify other and group (o and g) and use a minus sign to indicate that you want to turn off certain permissions for these
categories. Then, you used read (r) to indicate that you're specifically turning off read access. Now, the owner (tuser) can still read and write
the file, but all other users on the system (except the superuser) can't access the file. Note: The superuser (root) can override all file
permissions.
Listing 1. Example of bad habit #1: Defining directory trees individually
~ $ mkdir tmp
~ $ cd tmp
~/tmp $ mkdir a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $
It is so much quicker to use the -p option to mkdir and make all parent directories along with their
children in a single command. But even administrators who know about this option are still caught
stepping through the subdirectories as they make them on the command line. It is worth your time to
conscientiously pick up the good habit:
Listing 2. Example of good habit #1: Defining directory trees with one command
~ $ mkdir -p tmp/a/b/c
Copying & Moving Files and Directories
Feeding find output to pipes with xargs
One of the biggest limitations of the -exec command is that it can only run the specified command on one file at a time.
The xargs command solves this problem by enabling users to run a single command on many files at one time. In general, it is much faster to
run one command on many files, because this cuts down on the number of commands that need to bestarted.
For example often one needs to find files containing a specific pattern in multiple directories one can use an exec option in find (please note
that you should use the -l flag for grep so that grep specifies the matched filenames):
find . -type f -exec grep -l -i '/bin/ksh' {} ;
But there is more elegant and more Unix-like way of accomplishing the same task using xarg and pipes. You can use the
xargs to read the output of find and build a pipelines that envokes grep. This way, grep is called only four or five times
even though it might check through 200 or 300 files. By default, xargs always appends the list of filenames to the end of
the specified command, so using it is as easy as can be:
find . -type f -print | xargs grep -l -i 'bin/ksh'
Searching Directories - FIND
This gave the same output, but it was a lot faster. Also when grep is getting multiple filenames, it will automatically
include the filename of any file that contains a match when grep shows the matching line. Removing the -l flag results in
more meaningful (and potentially more useful output):
find . -type f -print | xargs grep -i 'bin/ksh'
When used in combination, find, grep, and xargs are a potent team to help find files lost or misplaced anywhere in the
UNIX file system. I encourage you to experiment further with these important commands to find ways they can help you
work with UNIX. You can use time to find the fifference in sleed with -exec option:
time find /usr/src -name "*.html" -exec grep -l foo '{}' ';' | wc -l
time find /usr/src -name "*.html" | xargs grep -l foo | wc –l
xargs works considerably faster. The difference becomes even greater when more complex commands are run and the list of files is longer.
find /mnt/zip -name "*prefs copy" -print | xargs rm
This won't work because I have a filename with spaces. If I add -print0, I can do it with no problems:
find /mnt/zip -name "*prefs copy" -print0 | xargs rm
Searching Directories - FIND
GREP command syntax
grep options searchterm filename
OR
command | grep options searchterm
For searching a word in file
grep 'ada' filename
For search a word which is either caps or small letters(i — case insensitive )
grep -i 'ac' filename
inverse search for a word
grep -v 'ac' filename
To count a word occurrence
grep -c ‘ac’ filename
Find ac characters along line numbers
grep -n 'ac' filename
Find exact word ac
grep -x 'ac' filename
Basic regular expressions:
^ -- Caret symbol, Match beginning of the line.
$ -- Match End of the line
* -- Match 0 or more occurrence of previous character .
? – Match Any single character
[] – Match Range of characters, just single occurrence.
[a-z] –Match small letters
[A-Z] –Match cap letters
[0-9] – Match numerical.
[^] – Match Negate a sequence
 -- Match Escape character.
Searching Files - GREP
Changing Permissions (chmod)
Use the chmod command to change permissions for a file or directory. You must be the owner of a file or directory, or have root access, to change its permissions. The
general form of the chmod command is:
chmod permissions name
In this example, permissions indicates the permissions to be changed and name is the name of the affected file or directory.
You can specify the permissions in several ways. Here is one of the forms that is easy to use:
Use one or more letters to indicate the type of users.
u (for the user)
g (for group)
o (for others)
a (for all three of the previous categories.))
Indicate whether the permissions are to be added (+) or removed (-).
Use one or more letters to indicate the permissions.
r (for read)
w (for write)
x (for execute)
In the following example, write permission is added to the directory carrots for users who belong to the same group (thus, permissions is g+w and name is carrots).
$ cd veggies2
$ ls -l
drwxr-xr-x 2 user2 users 512 Nov 1 09:11 carrots
$ chmod g+w carrots
$ ls -l drwxrwxr-x 2 user2 users 512 Nov 1 09:11 carrots
$
Now, the r (for read) and the x (for execute) in the set of permissions for other users are both changed to hyphens (-).
When you create a new file, the system automatically assigns the following permissions.
-rw-r--r--
When you create a new directory, the system automatically assigns the following permissions.
drwxr-xr-x
For example, to make a new file turnip executable by its owner (user2), type the following command.
$ ls -l turnip
-rw-r--r-- 1 user2 users 124 Nov 1 09:14 turnip
$ chmod u+x turnip
$ ls -l
turnip -rwxr--r-- 1 user2 users 124 Nov 1 09:14 turnip
$
File Permissions
If you want to change permissions for all categories of users, use the -a option of the ls command. To make a new file garlic executable by
everyone, type the following command.
$ ls -l garlic
-rw-r--r-- 1 user2 users 704 Nov 1 09:16 garlic
$ chmod a+x garlic
$ ls -l garlic
-rwxr-xr-x 1 user2 users 704 Nov 1 09:16 garlic
$
The x in the output of the ls -l command indicates garlic is executable by everyone.
You can also use the * wildcard character to change permissions for groups of files and directories. For example, to change the permissions
for all the files in the current directory veggies so that the files can be written by you alone, type the following command.
$ pwd
/home/user2/veggies
$ ls -l
-rwxrwxrwx 1 user2 users 5618 Nov 1 09:18 beets
-rwxrwxrwx 1 user2 users 1777 Nov 1 09:18 corn
-rwxrwxrwx 1 user2 users 3424 Nov 1 09:18 garlic
-rwxrwxrwx 1 user2 users 65536 Nov 1 09:18 garlic
-rwxr-xr-x 1 user2 users 65536 Nov 1 09:18 onions
$ chmod go-w *
$ ls -l
total 152
-rwxr-xr-x 1 user2 users 5618 Nov 1 09:18 beets
-rwxr-xr-x 1 user2 users 1777 Nov 1 09:18 corn
-rwxr-xr-x 1 user2 users 3424 Nov 1 09:18 garlic
-rwxr-xr-x 1 user2 users 65536 Nov 1 09:18 onions
File Permissions
Working with editors - vi
Command Description
vi filename Open or create file
vi Open new file to be named later
vi -r filename Recover crashed file
view filename Open file read-only
G Go to last line of file
1G Go to first line of file
21G Go to line 21
:set ic Searches should ignore case
:set noic Searches should be case sensitive
:set nu Show line numbers
:set nonu Hide line numbers
/string Search for string
?string Search backward for string
n Find next occurrence of string in search direction
N Find previous occurrence of string in search direction
:g/search/s//replace/g Search and replace
:w Save changes (write buffer)
:w filename Write buffer to named file
:wq Save changes and quit vi
ZZ Save changes and quit vi
:q! Quit without saving changes
In Unix, links allow more than one file name to refer to the same file.In windows terminology,we use to call it as shortcut. (soft
link).Link will not consume any disk space.It will just refer the source file.In Solaris,if you want to see soft links just go to /etc/rc2.d or
/etc/rc3.d directory.You can find most links on those directories and it will point to /etc/init.d files.For an example for hardlink
is /etc/inet/hosts & /etc/hosts.
syntax to create a hardlink:
# ln soure_file new_file
Hard Link:
This type of link files will share the same inode number with original files.In other words, “file” and hard link will share the physical data.
bash-3.00# ln /var/tmp/brerr /var/brerr_hardlink
bash-3.00# ls -l /var/brerr_hardlink
-rw-r--r-- 2 root root 304 Jul 17 14:12 /var/brerr_hardlink
bash-3.00# file /var/brerr_hardlink
/var/brerr_hardlink: ascii text
In the below example, i have created hard link “brerr_hardlink” for file “brerr” which is located under /var/tmp.
Both files are reading the same data location.
bash-3.00# cat /var/tmp/brerr
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
Editable data also available since its pointing to same inode.
bash-3.00# vi /var/tmp/brerr
"/var/tmp/brerr" 8 lines, 304 characters
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges
:wq!
Linking files with symbolic link and hard link
bash-3.00#
bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges
If you remove the original file ,it will just remove the file name not inode. So still you can access to the hard link file.
bash-3.00# rm /var/tmp/brerr
bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges
Limitations of hard link.
You can not create hard link across the filesystem.
Symbolic (soft) link:
Soft link is nothing but shortcut in windows terminology .If you remove the source file , you soft link will become invalid.
syntax:
# ln -s soure_file new_file
Here I am creating soft link for file "VRTS6.0.tar" in /opt with name of "tar_file“
bash-3.00# ln -s /var/tmp/VRTS6.0.tar /opt/tar_file
bash-3.00# file /opt/tar_file
/opt/tar_file: USTAR tar archive
bash-3.00# ls -l /opt/tar_file
lrwxrwxrwx 1 root root 20 Jul 25 18:16 /opt/tar_file -> /var/tmp/VRTS6.0.tar
Advantage over hard-link:
You can create soft link across the filesystem .
Linking files with symbolic link and hard link
Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)
Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Scheduling Jobs
Run jobs “at” specific times:
Sometimes you may need to run a job just once, rather than regularly. For this you use the at command. The commands to be run are read from a file specified
with the -f option, or from stdin if -f is not used. The -m option sends mail to the user even if there is no stdout from the command. The -v option will display the
time at which the job will run before reading the job. The time is also displayed in the output.
Listing 32 shows an example of running the mycrontest.sh script that you used earlier. Listing 33 shows the output that is mailed back to the user after the job
runs.
There is also a batch command, which is similar to the at command except that jobs are run only when the system load is low enough.
Listing scheduled jobs
You can manage your cron and at jobs. Use the crontab command with the –l option to list your crontab, and use the atq command to display the jobs you have
queued using the at command.
Deleting scheduled jobs
To delete system cron or anacron jobs, edit /etc/crontab, /etc/anacrontab, or edit or delete files in the /etc/cron.d directory.
You can delete one or more jobs that were scheduled with the at command by using the atrm command with the job number. Multiple jobs should be separated
by spaces.
Configure user access to job scheduling
If the file /etc/cron.allow exists, any non-root user must be listed in it in order to use crontab and the cron facility. If /etc/cron.allow does not exist, but
/etc/cron.deny does exist, a non-root user who is listed in it cannot use crontab or the cron facility.
If neither of these files exists, only the super user will be allowed to use this command. An empty /etc/cron.deny file allows all users to use the cron facility and is
the default.
The corresponding /etc/at.allow and /etc/at.deny files have similar effects for the at facility.
The ps command produces a report summarizing execution statistics for current processes. The bare ps command lists the process ID, the terminal the command was started
from, how much CPU time it has used, and the command itself. The output looks something like this (it differs from system to system):
PID TT STAT TIME COMMAND
1803 p5 IW 0:00 -csh (csh)
1883 p5 IW 0:04 vi outline
1811 p6 IW 0:01 -csh (csh)
5353 p6 TW 0:01 vi 4890
By default, ps lists only your own processes. There are many times, though, when it's desirable to have a more complete listing with a lot of data about all of the processes
currently running on the system. The options required to do this differ between BSD UNIX and System V. Under BSD UNIX, the command is ps -aux, which produces a table of
all processes, arranged in order of decreasing CPU usage at the moment when the ps command was executed. [The -a option gives processes belonging to all users, -u gives a
more detailed listing, and -x includes processes that no longer have a controlling terminal (38.6). -TOR ] It is often useful to pipe this output to head (25.20), which will display
the most active processes:
% ps -aux | head -5
USER PID %CPU %MEM SZ RSS TTY STAT TIME COMMAND
martin 12923 74.2 22.5 223 376 p5 R 2:12 f77 -o foo foo.F
chavez 16725 10.9 50.8 1146 1826 p6 R N 56:04 g94 HgO.dat
The meanings of the fields in this output (as well as others displayed by the -l option to ps) are given in Table 38.1.
The first line of this output shows that user martin is running a FORTRAN compilation (f77). This process has PID (38.3) 12923 and is currently either running or runable. User
chavez's process (PID 16725), executing the program g94, is also running or runable, though at a lowered priority. From this display, it's obvious who is using most system
resources at this instant: martin and chavez have about 85% of the CPU and 73% of the memory between them. However, although it does display total CPU time, ps does not
average the %CPU or %MEM values over time in any way.
A vaguely similar listing is produced by the System V ps -ef command:
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 09:36:35 ? 0:00 sched
root 1 0 0 09:36:35 ? 0:02 /etc/init
...
gull 7997 1 10 09:49:32 ttyp3 0:04 csh
The columns hold the username, process ID, parent's PID (the PID of the process that created it), the current scheduler value, the time the process started, its associated
terminal, its accumulated CPU time, and the command it is running. Note that the ordering is by PID, not resource usage.
Locating & monitoring of processes with ps
• What is it?
• It is a popular Unix and Linux command that is often used by system administrators to send signals to a running process or
daemon. A signal is a form of inter-process communication that is used in the Unix and Linux operating systems to notify a
daemon that an event has occurred. One of the jobs of the operating system is to notify and interrupt the daemon and allow
the daemon to take action. This command can be sent from a shell terminal or from a shell script such as the Bourne, Korn,
Bash, or C shell.
•
What is it useful for?
• There are many uses for the kill command. System administrators commonly use it in scheduled at jobs or crontab jobs for
automation. One of the most common uses is to terminate a daemon that appears to be hung up. SIGTERM and SIGKILL
signals are used with the kill command to terminate a daemon.
• SIGTERM signal is more graceful than SIGKILL because it nicely requests the termination of a daemon. SIGTERM gives the
daemon an opportunity to cleanup and close files before it begins to terminate. SIGTERM can be caught or ignored by a
daemon. For this reason, SIGTERM is normally used in init command to perform a graceful reboot or shutdown before it
uses SIGKILL. Unlike SIGTERM, a SIGKILL signal forces the daemon to stop immediately. A SIGKILL signal can not be caught or
ignored.
•
The Syntax
• The kill command is implemented slightly different between different flavors of the Unix and Linux operating systems.
However, they share one common syntax.
• Syntax: kill -n PID
• Where -n is the signal number, and PID is the daemon ID. The signal number can be any of the following:
• 1 - SIGHUP: hang up
2 - SIGINT: interrupt
3 - SIGQUIT: quit
6 - SIGABRT: abort
9 - SIGKILL: immediately terminate a daemon; signal can not be caught or ignored
14 - SIGALRM: alarm clock
15 - SIGTERM: software termination signal
Killing processes

More Related Content

What's hot (19)

Unix Basics For Testers
Unix Basics For TestersUnix Basics For Testers
Unix Basics For Testers
 
Treebeard's Unix Cheat Sheet
Treebeard's Unix Cheat SheetTreebeard's Unix Cheat Sheet
Treebeard's Unix Cheat Sheet
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
OpenGurukul : Operating System : Linux
OpenGurukul : Operating System : LinuxOpenGurukul : Operating System : Linux
OpenGurukul : Operating System : Linux
 
lec2.docx
lec2.docxlec2.docx
lec2.docx
 
Unix slideshare
Unix slideshareUnix slideshare
Unix slideshare
 
Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014
 
Linux cheat-sheet
Linux cheat-sheetLinux cheat-sheet
Linux cheat-sheet
 
Basic Linux commands
Basic Linux commandsBasic Linux commands
Basic Linux commands
 
One Page Linux Manual
One Page Linux ManualOne Page Linux Manual
One Page Linux Manual
 
Basic linux commands for bioinformatics
Basic linux commands for bioinformaticsBasic linux commands for bioinformatics
Basic linux commands for bioinformatics
 
Basic linux commands
Basic linux commands Basic linux commands
Basic linux commands
 
40 basic linux command
40 basic linux command40 basic linux command
40 basic linux command
 
Linux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for BeginnersLinux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for Beginners
 
MCLS 45 Lab Manual
MCLS 45 Lab ManualMCLS 45 Lab Manual
MCLS 45 Lab Manual
 
Linux Shell Basics
Linux Shell BasicsLinux Shell Basics
Linux Shell Basics
 
101 2.4 use debian package management
101 2.4 use debian package management101 2.4 use debian package management
101 2.4 use debian package management
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 

Similar to Unix Basics Commands (20)

Basics of Linux
Basics of LinuxBasics of Linux
Basics of Linux
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
Introduction to linux day-3
Introduction to linux day-3Introduction to linux day-3
Introduction to linux day-3
 
Linux presentation
Linux presentationLinux presentation
Linux presentation
 
Unix primer
Unix primerUnix primer
Unix primer
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
8.1.intro unix
8.1.intro unix8.1.intro unix
8.1.intro unix
 
linux-lecture4.ppt
linux-lecture4.pptlinux-lecture4.ppt
linux-lecture4.ppt
 
An Introduction to Linux
An Introduction to LinuxAn Introduction to Linux
An Introduction to Linux
 
Linux commands and file structure
Linux commands and file structureLinux commands and file structure
Linux commands and file structure
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
basic-unix.pdf
basic-unix.pdfbasic-unix.pdf
basic-unix.pdf
 
Basic Linux
Basic LinuxBasic Linux
Basic Linux
 
Lession1 Linux Preview
Lession1 Linux PreviewLession1 Linux Preview
Lession1 Linux Preview
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 

Recently uploaded

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

Unix Basics Commands

  • 1. UNIX – Training Directories and Files · File and Directories Creation, modifying and deleting · Copying & Moving Files and Directories · Searching Files and Directories – FIND & GREP · File Permissions · Working with editors - vi · Linking files - Soft links and Hard link · Scheduling Jobs - CRON · Locating a process and monitoring processes with ps · Killing processes - KILL
  • 2. File and Directories Creation, modifying and deleting Directory layout Now that you know how to move around directories and get listings, you're ready to look at the directory layout on a typical UNIX distribution. You can organize a UNIX file system several ways. This tutorial discusses a few root-level directories that are common to most UNIX-like distributions. There are other important root-level directories, but this is where you'll find yourself operating in most cases: /home (or /users) /etc /bin /sbin /usr /var /tmp Now, consider the possibility of having private information in this file that you don't want any other users to read. You probably want to remove read access for other groups and all other users. You can use chmod to change the permissions. Like many things in UNIX, there are multiple ways to use chmod; this section focuses on one. The three categories (user, group, and other) are represented by three letters (u, g, and o). The three types of permissions (read, write, and execute) are also represented by three letters (r, w, and x). To change permissions, use chmod followed by the letters of the categories you want to change, followed by a plus or a minus (for turn on or turn off), followed by the letters of the permissions you want to change. After this, write the name of the file (or files) you want to make the change to. This is best illustrated with an example: $ chmod og-r example.txt $ ls –l You should see this result: -rw------- 1 tuser admin 0 Aug 13 15:35 example.txt In this example, you specify other and group (o and g) and use a minus sign to indicate that you want to turn off certain permissions for these categories. Then, you used read (r) to indicate that you're specifically turning off read access. Now, the owner (tuser) can still read and write the file, but all other users on the system (except the superuser) can't access the file. Note: The superuser (root) can override all file permissions.
  • 3. Listing 1. Example of bad habit #1: Defining directory trees individually ~ $ mkdir tmp ~ $ cd tmp ~/tmp $ mkdir a ~/tmp $ cd a ~/tmp/a $ mkdir b ~/tmp/a $ cd b ~/tmp/a/b/ $ mkdir c ~/tmp/a/b/ $ cd c ~/tmp/a/b/c $ It is so much quicker to use the -p option to mkdir and make all parent directories along with their children in a single command. But even administrators who know about this option are still caught stepping through the subdirectories as they make them on the command line. It is worth your time to conscientiously pick up the good habit: Listing 2. Example of good habit #1: Defining directory trees with one command ~ $ mkdir -p tmp/a/b/c Copying & Moving Files and Directories
  • 4. Feeding find output to pipes with xargs One of the biggest limitations of the -exec command is that it can only run the specified command on one file at a time. The xargs command solves this problem by enabling users to run a single command on many files at one time. In general, it is much faster to run one command on many files, because this cuts down on the number of commands that need to bestarted. For example often one needs to find files containing a specific pattern in multiple directories one can use an exec option in find (please note that you should use the -l flag for grep so that grep specifies the matched filenames): find . -type f -exec grep -l -i '/bin/ksh' {} ; But there is more elegant and more Unix-like way of accomplishing the same task using xarg and pipes. You can use the xargs to read the output of find and build a pipelines that envokes grep. This way, grep is called only four or five times even though it might check through 200 or 300 files. By default, xargs always appends the list of filenames to the end of the specified command, so using it is as easy as can be: find . -type f -print | xargs grep -l -i 'bin/ksh' Searching Directories - FIND
  • 5. This gave the same output, but it was a lot faster. Also when grep is getting multiple filenames, it will automatically include the filename of any file that contains a match when grep shows the matching line. Removing the -l flag results in more meaningful (and potentially more useful output): find . -type f -print | xargs grep -i 'bin/ksh' When used in combination, find, grep, and xargs are a potent team to help find files lost or misplaced anywhere in the UNIX file system. I encourage you to experiment further with these important commands to find ways they can help you work with UNIX. You can use time to find the fifference in sleed with -exec option: time find /usr/src -name "*.html" -exec grep -l foo '{}' ';' | wc -l time find /usr/src -name "*.html" | xargs grep -l foo | wc –l xargs works considerably faster. The difference becomes even greater when more complex commands are run and the list of files is longer. find /mnt/zip -name "*prefs copy" -print | xargs rm This won't work because I have a filename with spaces. If I add -print0, I can do it with no problems: find /mnt/zip -name "*prefs copy" -print0 | xargs rm Searching Directories - FIND
  • 6. GREP command syntax grep options searchterm filename OR command | grep options searchterm For searching a word in file grep 'ada' filename For search a word which is either caps or small letters(i — case insensitive ) grep -i 'ac' filename inverse search for a word grep -v 'ac' filename To count a word occurrence grep -c ‘ac’ filename Find ac characters along line numbers grep -n 'ac' filename Find exact word ac grep -x 'ac' filename Basic regular expressions: ^ -- Caret symbol, Match beginning of the line. $ -- Match End of the line * -- Match 0 or more occurrence of previous character . ? – Match Any single character [] – Match Range of characters, just single occurrence. [a-z] –Match small letters [A-Z] –Match cap letters [0-9] – Match numerical. [^] – Match Negate a sequence -- Match Escape character. Searching Files - GREP
  • 7. Changing Permissions (chmod) Use the chmod command to change permissions for a file or directory. You must be the owner of a file or directory, or have root access, to change its permissions. The general form of the chmod command is: chmod permissions name In this example, permissions indicates the permissions to be changed and name is the name of the affected file or directory. You can specify the permissions in several ways. Here is one of the forms that is easy to use: Use one or more letters to indicate the type of users. u (for the user) g (for group) o (for others) a (for all three of the previous categories.)) Indicate whether the permissions are to be added (+) or removed (-). Use one or more letters to indicate the permissions. r (for read) w (for write) x (for execute) In the following example, write permission is added to the directory carrots for users who belong to the same group (thus, permissions is g+w and name is carrots). $ cd veggies2 $ ls -l drwxr-xr-x 2 user2 users 512 Nov 1 09:11 carrots $ chmod g+w carrots $ ls -l drwxrwxr-x 2 user2 users 512 Nov 1 09:11 carrots $ Now, the r (for read) and the x (for execute) in the set of permissions for other users are both changed to hyphens (-). When you create a new file, the system automatically assigns the following permissions. -rw-r--r-- When you create a new directory, the system automatically assigns the following permissions. drwxr-xr-x For example, to make a new file turnip executable by its owner (user2), type the following command. $ ls -l turnip -rw-r--r-- 1 user2 users 124 Nov 1 09:14 turnip $ chmod u+x turnip $ ls -l turnip -rwxr--r-- 1 user2 users 124 Nov 1 09:14 turnip $ File Permissions
  • 8. If you want to change permissions for all categories of users, use the -a option of the ls command. To make a new file garlic executable by everyone, type the following command. $ ls -l garlic -rw-r--r-- 1 user2 users 704 Nov 1 09:16 garlic $ chmod a+x garlic $ ls -l garlic -rwxr-xr-x 1 user2 users 704 Nov 1 09:16 garlic $ The x in the output of the ls -l command indicates garlic is executable by everyone. You can also use the * wildcard character to change permissions for groups of files and directories. For example, to change the permissions for all the files in the current directory veggies so that the files can be written by you alone, type the following command. $ pwd /home/user2/veggies $ ls -l -rwxrwxrwx 1 user2 users 5618 Nov 1 09:18 beets -rwxrwxrwx 1 user2 users 1777 Nov 1 09:18 corn -rwxrwxrwx 1 user2 users 3424 Nov 1 09:18 garlic -rwxrwxrwx 1 user2 users 65536 Nov 1 09:18 garlic -rwxr-xr-x 1 user2 users 65536 Nov 1 09:18 onions $ chmod go-w * $ ls -l total 152 -rwxr-xr-x 1 user2 users 5618 Nov 1 09:18 beets -rwxr-xr-x 1 user2 users 1777 Nov 1 09:18 corn -rwxr-xr-x 1 user2 users 3424 Nov 1 09:18 garlic -rwxr-xr-x 1 user2 users 65536 Nov 1 09:18 onions File Permissions
  • 9. Working with editors - vi Command Description vi filename Open or create file vi Open new file to be named later vi -r filename Recover crashed file view filename Open file read-only G Go to last line of file 1G Go to first line of file 21G Go to line 21 :set ic Searches should ignore case :set noic Searches should be case sensitive :set nu Show line numbers :set nonu Hide line numbers /string Search for string ?string Search backward for string n Find next occurrence of string in search direction N Find previous occurrence of string in search direction :g/search/s//replace/g Search and replace :w Save changes (write buffer) :w filename Write buffer to named file :wq Save changes and quit vi ZZ Save changes and quit vi :q! Quit without saving changes
  • 10. In Unix, links allow more than one file name to refer to the same file.In windows terminology,we use to call it as shortcut. (soft link).Link will not consume any disk space.It will just refer the source file.In Solaris,if you want to see soft links just go to /etc/rc2.d or /etc/rc3.d directory.You can find most links on those directories and it will point to /etc/init.d files.For an example for hardlink is /etc/inet/hosts & /etc/hosts. syntax to create a hardlink: # ln soure_file new_file Hard Link: This type of link files will share the same inode number with original files.In other words, “file” and hard link will share the physical data. bash-3.00# ln /var/tmp/brerr /var/brerr_hardlink bash-3.00# ls -l /var/brerr_hardlink -rw-r--r-- 2 root root 304 Jul 17 14:12 /var/brerr_hardlink bash-3.00# file /var/brerr_hardlink /var/brerr_hardlink: ascii text In the below example, i have created hard link “brerr_hardlink” for file “brerr” which is located under /var/tmp. Both files are reading the same data location. bash-3.00# cat /var/tmp/brerr Thu Jun 28 14:20:38 IST 2012. Java Accessibility Bridge for GNOME loaded. bash-3.00# cat /var/brerr_hardlink Thu Jun 28 14:20:38 IST 2012. Java Accessibility Bridge for GNOME loaded. Editable data also available since its pointing to same inode. bash-3.00# vi /var/tmp/brerr "/var/tmp/brerr" 8 lines, 304 characters Thu Jun 28 14:20:38 IST 2012. Java Accessibility Bridge for GNOME loaded. edited by linges :wq! Linking files with symbolic link and hard link
  • 11. bash-3.00# bash-3.00# cat /var/brerr_hardlink Thu Jun 28 14:20:38 IST 2012. Java Accessibility Bridge for GNOME loaded. edited by linges If you remove the original file ,it will just remove the file name not inode. So still you can access to the hard link file. bash-3.00# rm /var/tmp/brerr bash-3.00# cat /var/brerr_hardlink Thu Jun 28 14:20:38 IST 2012. Java Accessibility Bridge for GNOME loaded. edited by linges Limitations of hard link. You can not create hard link across the filesystem. Symbolic (soft) link: Soft link is nothing but shortcut in windows terminology .If you remove the source file , you soft link will become invalid. syntax: # ln -s soure_file new_file Here I am creating soft link for file "VRTS6.0.tar" in /opt with name of "tar_file“ bash-3.00# ln -s /var/tmp/VRTS6.0.tar /opt/tar_file bash-3.00# file /opt/tar_file /opt/tar_file: USTAR tar archive bash-3.00# ls -l /opt/tar_file lrwxrwxrwx 1 root root 20 Jul 25 18:16 /opt/tar_file -> /var/tmp/VRTS6.0.tar Advantage over hard-link: You can create soft link across the filesystem . Linking files with symbolic link and hard link
  • 12. Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax) Field Description Allowed Value MIN Minute field 0 to 59 HOUR Hour field 0 to 23 DOM Day of Month 1-31 MON Month field 1-12 DOW Day Of Week 0-6 CMD Command Any command to be executed. Scheduling Jobs Run jobs “at” specific times: Sometimes you may need to run a job just once, rather than regularly. For this you use the at command. The commands to be run are read from a file specified with the -f option, or from stdin if -f is not used. The -m option sends mail to the user even if there is no stdout from the command. The -v option will display the time at which the job will run before reading the job. The time is also displayed in the output. Listing 32 shows an example of running the mycrontest.sh script that you used earlier. Listing 33 shows the output that is mailed back to the user after the job runs. There is also a batch command, which is similar to the at command except that jobs are run only when the system load is low enough. Listing scheduled jobs You can manage your cron and at jobs. Use the crontab command with the –l option to list your crontab, and use the atq command to display the jobs you have queued using the at command. Deleting scheduled jobs To delete system cron or anacron jobs, edit /etc/crontab, /etc/anacrontab, or edit or delete files in the /etc/cron.d directory. You can delete one or more jobs that were scheduled with the at command by using the atrm command with the job number. Multiple jobs should be separated by spaces. Configure user access to job scheduling If the file /etc/cron.allow exists, any non-root user must be listed in it in order to use crontab and the cron facility. If /etc/cron.allow does not exist, but /etc/cron.deny does exist, a non-root user who is listed in it cannot use crontab or the cron facility. If neither of these files exists, only the super user will be allowed to use this command. An empty /etc/cron.deny file allows all users to use the cron facility and is the default. The corresponding /etc/at.allow and /etc/at.deny files have similar effects for the at facility.
  • 13. The ps command produces a report summarizing execution statistics for current processes. The bare ps command lists the process ID, the terminal the command was started from, how much CPU time it has used, and the command itself. The output looks something like this (it differs from system to system): PID TT STAT TIME COMMAND 1803 p5 IW 0:00 -csh (csh) 1883 p5 IW 0:04 vi outline 1811 p6 IW 0:01 -csh (csh) 5353 p6 TW 0:01 vi 4890 By default, ps lists only your own processes. There are many times, though, when it's desirable to have a more complete listing with a lot of data about all of the processes currently running on the system. The options required to do this differ between BSD UNIX and System V. Under BSD UNIX, the command is ps -aux, which produces a table of all processes, arranged in order of decreasing CPU usage at the moment when the ps command was executed. [The -a option gives processes belonging to all users, -u gives a more detailed listing, and -x includes processes that no longer have a controlling terminal (38.6). -TOR ] It is often useful to pipe this output to head (25.20), which will display the most active processes: % ps -aux | head -5 USER PID %CPU %MEM SZ RSS TTY STAT TIME COMMAND martin 12923 74.2 22.5 223 376 p5 R 2:12 f77 -o foo foo.F chavez 16725 10.9 50.8 1146 1826 p6 R N 56:04 g94 HgO.dat The meanings of the fields in this output (as well as others displayed by the -l option to ps) are given in Table 38.1. The first line of this output shows that user martin is running a FORTRAN compilation (f77). This process has PID (38.3) 12923 and is currently either running or runable. User chavez's process (PID 16725), executing the program g94, is also running or runable, though at a lowered priority. From this display, it's obvious who is using most system resources at this instant: martin and chavez have about 85% of the CPU and 73% of the memory between them. However, although it does display total CPU time, ps does not average the %CPU or %MEM values over time in any way. A vaguely similar listing is produced by the System V ps -ef command: $ ps -ef UID PID PPID C STIME TTY TIME CMD root 0 0 0 09:36:35 ? 0:00 sched root 1 0 0 09:36:35 ? 0:02 /etc/init ... gull 7997 1 10 09:49:32 ttyp3 0:04 csh The columns hold the username, process ID, parent's PID (the PID of the process that created it), the current scheduler value, the time the process started, its associated terminal, its accumulated CPU time, and the command it is running. Note that the ordering is by PID, not resource usage. Locating & monitoring of processes with ps
  • 14. • What is it? • It is a popular Unix and Linux command that is often used by system administrators to send signals to a running process or daemon. A signal is a form of inter-process communication that is used in the Unix and Linux operating systems to notify a daemon that an event has occurred. One of the jobs of the operating system is to notify and interrupt the daemon and allow the daemon to take action. This command can be sent from a shell terminal or from a shell script such as the Bourne, Korn, Bash, or C shell. • What is it useful for? • There are many uses for the kill command. System administrators commonly use it in scheduled at jobs or crontab jobs for automation. One of the most common uses is to terminate a daemon that appears to be hung up. SIGTERM and SIGKILL signals are used with the kill command to terminate a daemon. • SIGTERM signal is more graceful than SIGKILL because it nicely requests the termination of a daemon. SIGTERM gives the daemon an opportunity to cleanup and close files before it begins to terminate. SIGTERM can be caught or ignored by a daemon. For this reason, SIGTERM is normally used in init command to perform a graceful reboot or shutdown before it uses SIGKILL. Unlike SIGTERM, a SIGKILL signal forces the daemon to stop immediately. A SIGKILL signal can not be caught or ignored. • The Syntax • The kill command is implemented slightly different between different flavors of the Unix and Linux operating systems. However, they share one common syntax. • Syntax: kill -n PID • Where -n is the signal number, and PID is the daemon ID. The signal number can be any of the following: • 1 - SIGHUP: hang up 2 - SIGINT: interrupt 3 - SIGQUIT: quit 6 - SIGABRT: abort 9 - SIGKILL: immediately terminate a daemon; signal can not be caught or ignored 14 - SIGALRM: alarm clock 15 - SIGTERM: software termination signal Killing processes