SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Treebeard's Unix Cheat Sheet
Treebeard's Unix Cheat SheetTreebeard's Unix Cheat Sheet
Treebeard's Unix Cheat Sheet
wensheng wei
 
One Page Linux Manual
One Page Linux ManualOne Page Linux Manual
One Page Linux Manual
dummy
 
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
Acácio Oliveira
 

Tendances (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
 

Similaire à Unix Basics Commands

Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
Dima Gomaa
 
Linux presentation
Linux presentationLinux presentation
Linux presentation
Nikhil Jain
 
Unix primer
Unix primerUnix primer
Unix primer
dummy
 
8.1.intro unix
8.1.intro unix8.1.intro unix
8.1.intro unix
southees
 
An Introduction to Linux
An Introduction to LinuxAn Introduction to Linux
An Introduction to Linux
Dimas Prasetyo
 
Lession1 Linux Preview
Lession1 Linux PreviewLession1 Linux Preview
Lession1 Linux Preview
leminhvuong
 

Similaire à 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
 

Dernier

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
Muhammad Subhan
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Dernier (20)

Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 

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