ACFrOgBdFOgbtNWXn8CusxCTr3EuZzUjLk8dKSl1CaMNa6nT_EH7UVlB2QlducvVsLc75RSONZZ5nWC7FhzVrlZ3d8Yvz1KMzQGxpMZVyiCSfFHl80__LnIAAzPmjqU6NOciVOAXYvbLyWZZoVBGMLRleux-9Q20izBiA1wSFw==.pdf

Document Details

Uploaded by Deleted User

Tags

Unix commands shell scripting computer science programming

Full Transcript

Experiment 1: Here’s an overview of the basic Unix shell commands you've listed, along with their common usages: 1. ls (List Directory Contents)  Description: Lists files and directories within the current directory.  Common Options: o ls -l: Displays in long format, showing...

Experiment 1: Here’s an overview of the basic Unix shell commands you've listed, along with their common usages: 1. ls (List Directory Contents)  Description: Lists files and directories within the current directory.  Common Options: o ls -l: Displays in long format, showing file permissions, number of links, owner, group, file size, and timestamp. o ls -a: Shows hidden files (those starting with a dot). o ls -h: Displays file sizes in human-readable format (e.g., KB, MB). 2. mkdir (Make Directory)  Description: Creates a new directory.  Usage: o mkdir [directory_name]: Creates a new directory with the specified name. o mkdir -p [path]: Creates nested directories (e.g., mkdir -p /home/user/docs/2024). 3. rmdir (Remove Directory)  Description: Removes an empty directory.  Usage: o rmdir [directory_name]: Removes the specified directory if it is empty. o Note: To remove non-empty directories, use the rm -r command. 4. cd (Change Directory)  Description: Changes the current working directory.  Usage: o cd [directory_name]: Moves into the specified directory. o cd..: Moves up one directory level. o cd ~: Moves to the user's home directory. o cd -: Moves to the previous working directory. 5. cat (Concatenate and Display Files)  Description: Displays the contents of a file, concatenates multiple files, and can create new files.  Usage: o cat [file_name]: Displays the contents of the file. o cat [file1] [file2] > [newfile]: Concatenates file1 and file2 into newfile. o cat > [newfile]: Creates a new file with the specified name and allows you to type content directly (press Ctrl+D to save and exit). 6. banner (Create a Large Banner)  Description: Displays text in a large, ASCII-art style banner.  Usage: o banner [text]: Prints the specified text in a large banner. 7. touch (Change File Timestamps or Create Empty Files)  Description: Updates the access and modification timestamps of a file, or creates an empty file if it doesn't exist.  Usage: o touch [file_name]: Creates an empty file or updates the timestamp of an existing file. 8. file (Determine File Type)  Description: Identifies the type of a file (e.g., text file, binary, directory).  Usage: o file [file_name]: Displays the file type. 9. wc (Word, Line, Character, and Byte Count)  Description: Counts lines, words, characters, and bytes in a file.  Common Options: o wc -l: Counts lines. o wc -w: Counts words. o wc -c: Counts bytes. o wc [file_name]: Displays line, word, and byte counts for the specified file. 10. sort (Sort Text Files)  Description: Sorts the lines in a text file.  Usage: o sort [file_name]: Sorts the file alphabetically by default. o sort -r [file_name]: Sorts in reverse order. o sort -n [file_name]: Sorts numerically. 11. cut (Remove Sections from Each Line of Files)  Description: Extracts sections from each line of a file or input.  Usage: o cut -d [delimiter] -f [field_number] [file_name]: Extracts the specified field based on the delimiter. o cut -c [start]-[end] [file_name]: Extracts characters from specified positions. 12. dd (Convert and Copy a File)  Description: Converts and copies files, often used for low-level copying and disk cloning.  Usage: o dd if=[source_file] of=[destination_file]: Copies the source file to the destination file. o dd if=/dev/zero of=[file_name] bs=[block_size] count=[count]: Creates a file filled with zeros. 13. dfspace (Show Disk Space Usage) [Deprecated]  Description: Shows disk space usage. Note that dfspace is deprecated and df is used instead.  Usage: o df -h: Displays disk space usage in human-readable format. 14. du (Estimate File Space Usage)  Description: Displays the disk usage of files and directories.  Usage: o du -h: Displays sizes in human-readable format. o du -s [directory_name]: Shows the total size of a directory. 15. ulimit (User Limits)  Description: Sets or shows the user limits for system-wide resources.  Usage: o ulimit -a: Displays all current limits. o ulimit -c [size]: Sets the core file size limit. o ulimit -n [number]: Sets the limit on the number of open files. o Experiment 2: The grep command is used to search for patterns within files. It stands for "Global Regular Expression Print" and is one of the most powerful and widely used commands in Unix/Linux for text processing. Basic Syntax sh grep [options] pattern [file...]  pattern: The text or regular expression you want to search for.  file: The file or files you want to search through. If no file is specified, grep reads from the standard input (stdin). Common Options  -i: Ignore case (case insensitive).  -v: Invert match, i.e., select non-matching lines.  -r: Recursively search directories.  -n: Show line numbers of matches.  -c: Display count of matching lines.  -l: Show only the names of files with matches.  -w: Match whole words only.  -e: Specifies multiple patterns.  -A [num]: Display [num] lines after a match.  -B [num]: Display [num] lines before a match.  -C [num]: Display [num] lines around a match. Examples 1. Search for a specific word in a file: grep "word" filename.txt This command will print all lines in filename.txt that contain the word "word". 2. Case-insensitive search: grep -i "word" filename.txt This will match "word", "Word", "WORD", etc. 3. Search recursively in all files in a directory: grep -r "word" /path/to/directory This searches for "word" in all files in the specified directory and its subdirectories. 4. Display line numbers with matching lines: grep -n "word" filename.txt This will show the line number along with the matching lines. 5. Count the number of matching lines: grep -c "word" filename.txt This displays the number of lines that match the pattern. 6. Invert match (display non-matching lines): grep -v "word" filename.txt This will show all lines that do not contain the word "word". 7. Match whole words: grep -w "word" filename.txt This ensures that "word" is matched as a whole word, not as a substring (e.g., it will match "word" but not "wording"). 8. Search for multiple pattern: grep -e "pattern1" -e "pattern2" filename.txt This searches for both "pattern1" and "pattern2" in the file. 9. Display lines before/after a match: grep -A 2 "word" filename.txt This shows the matching line and 2 lines after it. grep -B 2 "word" filename.txt This shows the matching line and 2 lines before it. grep -C 2 "word" filename.txt This shows the matching line and 2 lines before and after it. 10. Search in multiple files and display filenames with matches: grep -l "word" *.txt This will show the names of all.txt files that contain the word "word". Regular Expressions with grep The real power of grep comes when you combine it with regular expressions to search for complex patterns. .: Matches any single character except a newline.  ^: Matches the start of a line.  $: Matches the end of a line.  *: Matches zero or more occurrences of the previous character.  \b: Matches a word boundary. For example, to find lines that start with "word": grep "^word" filename.txt To match lines that end with "word": grep "word$" filename.txt By mastering grep, you can efficiently search through files and directories, making it an essential tool for any Unix/Linux user.

Use Quizgecko on...
Browser
Browser