0% found this document useful (0 votes)
13 views6 pages

Essential Unix Commands Guide

The document provides information on common UNIX/Linux commands and their usage. It describes commands for listing files and directories (ls, pwd), viewing file contents (more, cat), searching files (grep, sed, cut), manipulating text (tr, awk), managing processes (ps, kill), file permissions (chmod), and more. Key points are that grep searches for words, sed searches rows/lines, and cut extracts columns or characters from a file. The document offers examples of using each command to make its usage clear.

Uploaded by

Kalyani Siva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Essential Unix Commands Guide

The document provides information on common UNIX/Linux commands and their usage. It describes commands for listing files and directories (ls, pwd), viewing file contents (more, cat), searching files (grep, sed, cut), manipulating text (tr, awk), managing processes (ps, kill), file permissions (chmod), and more. Key points are that grep searches for words, sed searches rows/lines, and cut extracts columns or characters from a file. The document offers examples of using each command to make its usage clear.

Uploaded by

Kalyani Siva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIX Hand Notes by Amarsoftech

wc -l: To find the number of lines

wc -w: To find the number of words

wc -c: To find the number of characters

more -n: The content of the complete file is displayed

ls: listing

ls -l: long listing

ls -a: hidden files will be displayed

ls -r: this gives the reverse order

ls -t: time when the file was created

ls -tr: time based + reverse order

ls -lart: long listing + hidden files + reverse order + time based

Head: Top to bottom

Tail: Bottom to top

Ps: list of processes

Ps tree: list of process running since long time and memory usage.

Kill: to kill a process/terminates the process

Kill -9 1286 (1286 is the file we want to kill)

Cmp: compares word by word

Diff: compares line by line

m -ls-l: memory size of file

cd: change directory

$ touch command: to create a empty file

.done: to create a 0 byte file


Chmod 777 filename: to give permissions to group, users and others

777-All Permissions

7-Group, 7-Users, 7-Others

1-Execute

2-Write

3-Execute/Write

4-Read

5-Read/Execute

6-Read/Write

7-Read/Write/Execute

Pwd: Present Working Directory

Whoami: to know which user am i

Awk command ---this for awake

CAT command:

CAT > filename: we can enter the values into the file

CAT >> filename: values entered into the file can be edited (only at the end of the
file)

CAT filename: we can view the file

Vi editor: Using vi editor also we can edit the values (here we can edit anywhere –
Beginning, Middle or end of the file)

Vi filename: to create a file

i – to go into insert mode

w - save the data

q – quit from existing mode

:wq! – save and quit


!q; - exit without saving data

Cut command -- It is a search option for COLUMN’S

Cut -f --- columns

Cut -c --- characters

Cut command for selecting Columns/Fields ($cut –f ‘number of columns we want’


filename)

$ cut -f n filename (in place of n you need to mention the number of the column
you want)

Example : $ cut –f 2-5 filename ---- we will get 2,3,4,5 columns

Cut command for selecting Characters (cut –c ‘number of characters we want’


filename)

Unix Cut Command Example


Lets consider the below as an example

> cat [Link]

unix or linux os

is unix good os

is linux good os

1. Cut command to print characters by position?

The cut command can be used to print characters in a line by specifying the
position of the characters. To print the characters in a line, use the -c option in
cut command
OUTPUT:
cut -c4 [Link]

If we give 4,6 we will get the below result

cut -c4,6 [Link]

xo

ui

ln

This command prints the fourth and sixth character in each line.

tr --- translator

tr ‘0’ ‘9’ <filename ----9 will be replaced in place of 0

Note: 0 is old value, 9 is new value

tr ‘a-z’ ‘A-Z’ <filename --- small letters will be replaced into CAPITAL LETTERS

tr -d ‘0-9’ <filename ----0 to 9 numbers gets deleted

tr -d ‘0-9’,‘a-z’ <filename ----particular relevant numbers and alphabets gets


deleted

SED Command---It is a search option for ROWS (Sed -n ‘parameter number’


filename)
SED -n ‘25p’ filename this gives the 25th row (Here p is the parameter)

SED -n ‘11p – 19p’ filename this gives the rows from 11 to 19 (Here p is the
parameter)

GREP Command --It is a search option for WORDS (grep -n ‘word’ filename)

grep-n: this shows the number of times a particular word is present in a


paragraph or a page

Example: grep -n ‘word we want to search’ filename

Grep –ni (Here i ignores case sensitive)

Example : If we want to search the amount 50k?

If there is 50k (with a small letter k)

AND

If there is 50K (with a capital letter K)

In this case if you mention i it ignores case sensitive and gives all the 50k values (it
will have both 50k and 50K).

Grep Command (WITHOUT USING ni)

Grep ‘is’ filename – we will get all the records which has the word ‘is’ from that
particular file

Grep ‘rahul’ * ----- we will get all the records which has rahul from all the files.

Grep -l ----we will get all the filenames

Cal | (pipe symbol) tee filename ----Here it gives the result and copies the data
to another file.
Points to Remember

GREP  WORDS (-n)

SED  SENTENCES (-n)

CUT  COLUMNS (-f) f is for columns (-c) c is for characters

Common questions

Powered by AI

'Awk' is a versatile programming language designed for advanced text manipulation and data extraction by processing columns within a text, using relational tests and arithmetic operations . It excels at tasks that require pattern scanning and processing data based on those patterns. 'Sed,' on the other hand, is a stream editor primarily used for parsing and transforming text in a more sequential manner, suitable for basic text substitution, deletion, and insertion tasks on a line-by-line basis . 'Awk' is powerful in scenarios where multi-field data processing and complex formatting are necessary, such as data reports or log file analysis, while 'sed' is preferable for simpler scripts with straightforward substitutions or edits.

The 'tr' command is used for translating or deleting characters in a text file. It operates on standard input and requires both the source and target character set to be provided for translation. For example, 'tr 'a-z' 'A-Z'' will convert lowercase letters to uppercase in a given file . It also allows character deletion with the '-d' option, such as 'tr -d '0-9'' which removes all digits from the input text . Practical use cases include formatting text to maintain consistency (e.g., converting case) and cleaning up data by removing unwanted characters like digits or punctuation.

The 'cut' command with the '-f' option is used for extracting fields from a file based on a delimiter, suitable for columns in a CSV file where processing specific data fields is necessary . For example, 'cut -f 2-5' extracts columns 2 to 5 from the data. Conversely, the '-c' option extracts specific character positions from each line, useful in scenarios where fixed-position text formats are involved, and a precise character count is needed, such as extracting specific bytes for encoding operations . Each option serves distinct purposes, with '-f' fitting columnar data and '-c' aligning with character-centric processing.

The 'tee' command is used within pipelines to simultaneously write command output to a file and pass it to the next stage in the pipeline. It effectively clones the output stream, which facilitates logging or archiving a copy of processed data while still conducting further operations without re-running the initial command. For instance, using 'cal | tee output.txt' records the calendar output in 'output.txt' while also displaying it on the console, enhancing data transparency and verification processes . This command is crucial in data processing pipelines that require both real-time data transformation and persistent logging.

Using 'head' and 'tail' together through a pipeline is advantageous when needing to extract and view a specific range of lines from a large file, without manually scrolling through content. For instance, to display lines 10 to 20 of a file, 'head -n 20 filename | tail -n 11' can be used. 'Head' extracts the first 20 lines, while 'tail' then trims these down to start at line 10, producing the needed line range efficiently . This approach is particularly useful in large log files where viewing contiguous but non-initial or non-terminal sections is required.

The 'grep' command can be optimized for case-insensitive searches with the '-i' option, which ignores case distinctions. To search across multiple files, the wildcard '*' can be used to specify all files in the directory. For example, 'grep -i 'search_term' *' will return matches regardless of case from all lines in the files . This approach is useful when consistent text patterns are sought in diverse file sets, and case differences are irrelevant to the search criteria.

The 'ps tree' command provides a hierarchical view of processes, showing relationships between parent and child processes, which is particularly useful for diagnosing system performance issues or tracing process parentage. In contrast, 'ps' alone lists processes without showing any hierarchy. For example, when troubleshooting why certain child processes consume excessive resources, 'ps tree' can quickly illustrate the responsible parent process and structure, helping administrators understand dependency and hierarchy within the system's running tasks .

The 'chmod' command in UNIX alters file permission settings using either symbolic or numeric modes. In numeric mode, permissions are set using an octal number that represents the sum of user, group, and others' permissions. Each permission level (read, write, execute) corresponds to a specific value (4, 2, and 1, respectively). For example, 'chmod 777 filename' grants all permissions to everyone. In symbolic mode, permissions are modified using characters and operators that indicate whom the changes apply to (e.g., 'u' for user, 'g' for group, 'o' for others), the kind of permission (+ to add, - to remove), and the permission type (r, w, x). An example would be 'chmod u+x filename' to add execution permissions to the user.

The 'cmp' command in UNIX compares two files byte by byte and is typically used to identify the first point of difference, giving no detailed output beyond that difference . This makes 'cmp' suitable for checking if two files are identical or determining the initial difference point in large files without needing to know every difference. On the other hand, 'diff' compares files line by line and is more informative, providing a detailed report of all differences . 'Diff' is ideal for comparing text files where you need to see all the differences line by line, making it highly useful in source code version control and change tracking situations.

The command 'ls -lart' combines long listing, reverse order, hidden files display, and modification time sorting, providing a comprehensive view of file changes in a directory. It shows file permissions, ownership, size, and modification timestamps, which is beneficial for auditing changes, particularly in directories under collaborative development or management . This command is most valuable in identifying newly modified or accessed files at the bottom of the listing, providing insights into recent activities that may impact consistency or security.

You might also like