Cosc 224 Cat 1. Commands
Cosc 224 Cat 1. Commands
UNIX COMMANDS
GROUP MEMBERS
Useful for:
Programming Constructs:
Syntax
awk options 'selection _criteria {action}' input-file > output-file
Awk is a scripting language used for manipulating data and generating reports. It searches one
or more files to see if they contain lines that matches with the specified patterns and then
perform the associated actions.
Examples
Default behavior of Awk: By default, Awk prints every line of data from the specified file.
$ awk '{print}' employee.txt
Output:
ajay manager account 45000 sunil
clerk account 25000 varun manager
sales 50000 amit manager account
47000 tarun peon sales 15000
Deepak clerk sales 23000 sunil
peon sales 13000 satvik director
purchase 80000
In the above example, no pattern is given. So the actions are applicable to all the lines. Action
print without any argument prints the whole line by default, so it prints all the lines of the file
without failure.
Options
Description
-c
This prints only a count of the lines that match a pattern
We can find the number of lines that matches the given string/pattern output
administrator@maggie- laptop: ~$ grep –c
–i
Ignores, case for matching.
The -i option enables to search for a string case insensitively in the given file. It matches the
words like “UNIX”, “Unix”, “unix”.
output
administrator@maggie-laptop: ~$ grep -i "unix" geekfile.txt Unix is great
-l
Displays list of a filenames only.
We can just display the files that contains the given string/pattern.
outadminstrator@maggie-laptop: ~$ grep -l "unix" * grep:
Desktop: is a directory grep: Documents: is a directory grep:
Downloads: is a directory
-n
Display the matched lines and their line numbers.
output
administrator@maggie-laptop: ~$ grep -n "unix" geekfile.txt 1: unix is
great os. unix was developed in bell labs.
4: unix is easy to learn. Unix is a multiuser os. learn unix. unix is a powerful.
administrator@maggie-laptop: ~$
-v
This prints out all the lines that do not matches the pattern
-e exp
Specifies expression with this option. Can use multiple times.
-f file
Takes patterns from file, one per line.
-E
Treats pattern as an extended regular expression (ERE)
-w
Match whole word output
administrator@maggie-laptop: ~$ grep -w "unix" geekfile.txt unix is great Os. unix
was developed in bell labs. unix is easy to learn. Unix is a multiuser os. learn unix.
unix is a powerful.
administrator@maggie-laptop: ~$
-o
Print only the matched parts of a matching line, with each such part on a separate output line.
Displaying only the matched pattern Using grep
By default, grep displays the entire line which has the matched string. We can make the grep to
display only the matched string by using the -o option.
C. fmt COMMAND
The fmt command in Linux/Unix is used to format text by adjusting line lengths to make it more
readable. Here's a detailed explanation, with examples:
FMT COMMAND
Function: The fmt command is used to format plain text by adjusting the width of lines to make
them more uniform. It's especially useful for reformatting paragraphs or text files into neat,
readable lines of a given length.
Syntax:
If you do not specify a file, fmt will read from the standard input (i.e., the terminal or a pipe).
Examples:
1. Basic Usage (Formatting Text) Command:
echo "This is a very long sentence that will be automatically wrapped at the default
width of 75 characters." | fmt Output:
This is a very long sentence that will be automatically wrapped at the default width of
75 characters.
Explanation: The sentence is automatically wrapped to fit within 75 characters per line, which is
the default width used by fmt.
echo "This is a very long sentence that will be automatically wrapped at the specified width of
40 characters." | fmt -w 40
Output:
This is a very long sentence that will be automatically
wrapped at the specified width of 40 characters.
Explanation: The -w option specifies the maximum width of the lines (in this case, 40
characters). The text is wrapped accordingly.
fmt example.txt
Output: (Assuming example.txt contains multiple lines of text, fmt will format the text,
adjusting the line lengths for readability.)
Explanation: If the file example.txt contains multiple paragraphs, fmt will reformat the text to
have consistent line lengths.
Output: (Assuming example.txt contains long lines of text, the content will be formatted to
make lines more readable.)
Common Options for fmt
-w WIDTH
Set the width of the formatted lines. The default is 75 characters.
-c
Use the default width (75 characters), but leave blank lines between paragraphs.
-s
Do not break lines that are already shorter than the specified width.
-p
Don’t join lines unless necessary (useful for formatting text without merging short lines).
1. Basename
The ‘basename’ command in Linux is a fundamental utility used in file manipulation and script
writing. It simplifies file paths by stripping directory information and optional suffixes from file
names.
Syntax of ‘basename’ command
$basename NAME [SUFFIX]
or
$basename OPTION NAME
‘basename’ command and in case, if you want to strip off the suffix of a file you can give the
filename followed by the SUFFIX name you want to get rid off.
Example:
= basename removes directory path and returns the file
name, kt
$basename /usr/local/bin/kt
Kt
In the first case, the ‘basename’ command removes the directories name from the full path
name given of file ‘kt’ and in the second case ‘basename’ removes the ‘suffix.html’ from
‘kt.html’.
Options for basename command:
‘-a, – -multiple’ option : This option lets you support multiple arguments and treat each as a
NAME
-a is used for multiple inputs
$basename -a /usr/local/bin/kt /usr/local/bin/kt.html
kt kt.html
‘-s, – -suffix = SUFFIX’ option: This option removes a trailing suffix SUFFIX, such as a file
extension.
3. Base64
The base64 command in Unix is used for encoding and decoding data in Base64 format. This
encoding helps in converting binary data into an ASCII string, which is useful for storing or
transmitting data safely.
Example Uses
1. Encoding a String:
echo "Hello, World!" | base64
Output:
SGVsbG8sIFdvcmxkIQo=
3. Encoding a File:
6. Use bc interactively
Just type bc and press Enter. You can then enter calculations line by line.
Basic Usage
fg %1
(where %1 is the job number from jobs)
7. Bind
Set or display readline key and function bindings.
Bind command is a Bash shell builtin command. It is used to set Readline key bindings and
variables. The keybindings are the keyboard actions that are bound to a function. So it can be
used to change how the bash will react to keys or combinations of keys, being pressed on the
keyboard. •
Syntax
bind [-lpsvPSVX] [-m keymap] [-q name] [-f filename] [-u name] [-r keyseq] [-x keyseq:
shell-command] [keyseq: readline-function or readline-command] where,
keyseq: This refers to the key sequence or combination of keys that you want to bind to a
specific function or command. read line-function: These are functions provided by the Readline
library, like moving the cursor, deleting characters, or cutting and pasting text.
shell-command: You can bind key sequences to execute shell commands when pressed.
8. Break command
In Linux, the break command is used in shell scripting (like Bash) to exit a loop prematurely, just
like in programming languages like Python. When the break command is encountered inside a
loop (e.g., for, while, or until), the loop is terminated, and the script continues to execute the
next statement after the loop.
Example of using break in a shell script:
#!/bin/bash
counter=0
while [ $counter -lt 10 ] do
((counter++)) echo
"Counter: $counter"
if [ $counter -eq 5 ]; then
echo "Breaking the loop at counter = $counter" break
fi
done
echo "Loop ended." Explanation:
A while loop runs while $counter is less than 10.
Each time through the loop, it increments $counter and prints the value.
When $counter equals 5, the break command is triggered, terminating the loop. After the loop
ends, the script prints "Loop ended."
Output: vbnet
Counter: 1 Counter: 2
Counter: 3 Counter: 4
Counter: 5
Breaking the loop at counter = 5 Loop
ended.
In this example:
The loop prints the value of counter until it reaches 5, and at that point, it exits the loop due to
the break statement.
After the loop is terminated, the script prints "Loop ended."
9. Builtin
In Linux, a builtin refers to a command that is built directly into the shell rather than being
an external executable.
Syntax
builtin [shell-builtin [arg ..]]
Common Builtin Commands in Bash:
cd – Change directory
echo – Print text to standard output pwd –
Print working directory