SlideShare a Scribd company logo
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)

DOC
Unix Basics For Testers
nitin lakhanpal
 
DOC
Treebeard's Unix Cheat Sheet
wensheng wei
 
PDF
Basic linux commands
Harikrishnan Ramakrishnan
 
ODP
OpenGurukul : Operating System : Linux
Open Gurukul
 
DOCX
lec2.docx
ismailaboshatra
 
PPTX
Unix slideshare
Mohan Krishna Kona
 
PDF
Unix Command-Line Cheat Sheet BTI2014
Noé Fernández-Pozo
 
PDF
Linux cheat-sheet
Craig Cannon
 
PDF
Basic Linux commands
atozknowledge .com
 
PDF
One Page Linux Manual
dummy
 
PDF
Basic linux commands for bioinformatics
Bonnie Ng
 
PDF
Basic linux commands
Raghav Arora
 
DOCX
40 basic linux command
Teja Bheemanapally
 
PDF
Linux Bash Shell Cheat Sheet for Beginners
Davide Ciambelli
 
PDF
MCLS 45 Lab Manual
Lokesh Singrol
 
PPTX
Linux Shell Basics
Constantine Nosovsky
 
PPT
101 2.4 use debian package management
Acácio Oliveira
 
PPT
Linux
sravan kumar
 
Unix Basics For Testers
nitin lakhanpal
 
Treebeard's Unix Cheat Sheet
wensheng wei
 
Basic linux commands
Harikrishnan Ramakrishnan
 
OpenGurukul : Operating System : Linux
Open Gurukul
 
lec2.docx
ismailaboshatra
 
Unix slideshare
Mohan Krishna Kona
 
Unix Command-Line Cheat Sheet BTI2014
Noé Fernández-Pozo
 
Linux cheat-sheet
Craig Cannon
 
Basic Linux commands
atozknowledge .com
 
One Page Linux Manual
dummy
 
Basic linux commands for bioinformatics
Bonnie Ng
 
Basic linux commands
Raghav Arora
 
40 basic linux command
Teja Bheemanapally
 
Linux Bash Shell Cheat Sheet for Beginners
Davide Ciambelli
 
MCLS 45 Lab Manual
Lokesh Singrol
 
Linux Shell Basics
Constantine Nosovsky
 
101 2.4 use debian package management
Acácio Oliveira
 

Similar to Unix Basics Commands (20)

PPT
Linux ppt
Sanmuga Nathan
 
PDF
Unix command line concepts
Artem Nagornyi
 
PDF
basic-unix.pdf
OmprakashNath2
 
PPTX
Linux Commands all presentation file .pptx
AshutoshPrajapati30
 
PPTX
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
 
PPTX
various shell commands in unix operating system.pptx
ssuserc26f8f
 
PPT
Common linux ubuntu commands overview
Ameer Sameer
 
TXT
Unix3
Krishna Prasad
 
PPTX
Ai module
KUMARRISHAV29
 
PPT
Learning Linux v2.1
sdiviney
 
PDF
3.1.a linux commands reference
Acácio Oliveira
 
PPT
Unix fundamentals
Dima Gomaa
 
PPTX
Topic 3-1_More_Linux_Commands.pptx
dulala3
 
PPT
Bootcamp linux commands
NexThoughts Technologies
 
PPT
Unix Basics 04sp
Dr.Ravi
 
DOC
58518522 study-aix
homeworkping3
 
DOCX
Basic linux commands
Dheeraj Nambiar
 
PPTX
Unix Trainning Doc.pptx
KalpeshRaut7
 
PPT
8.1.intro unix
southees
 
PDF
Unix / Linux Command Reference
Sumankumar Panchal
 
Linux ppt
Sanmuga Nathan
 
Unix command line concepts
Artem Nagornyi
 
basic-unix.pdf
OmprakashNath2
 
Linux Commands all presentation file .pptx
AshutoshPrajapati30
 
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
 
various shell commands in unix operating system.pptx
ssuserc26f8f
 
Common linux ubuntu commands overview
Ameer Sameer
 
Ai module
KUMARRISHAV29
 
Learning Linux v2.1
sdiviney
 
3.1.a linux commands reference
Acácio Oliveira
 
Unix fundamentals
Dima Gomaa
 
Topic 3-1_More_Linux_Commands.pptx
dulala3
 
Bootcamp linux commands
NexThoughts Technologies
 
Unix Basics 04sp
Dr.Ravi
 
58518522 study-aix
homeworkping3
 
Basic linux commands
Dheeraj Nambiar
 
Unix Trainning Doc.pptx
KalpeshRaut7
 
8.1.intro unix
southees
 
Unix / Linux Command Reference
Sumankumar Panchal
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Ad

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