Frequently used linux commands
ls command
The syntax for the ls command is as follows:
ls [OPTIONS] [FILES]
The default output of the ls command shows only the names of the files and directories.
The -l option tells ls to print files in a long listing format.
The file types.
The file permissions.
Number of hard links to the file.
File owner.
File group.
File size.
Date and Time.
File name.
Sample output below :
chmod command
Purpose:
chmod stands for "change mode" and is used to modify the permissions of files and directories.
It controls who can read, write, and execute (run) files or access and traverse directories.
Key Concepts:
Permissions: Each file or directory has three sets of permissions:
Owner: The user who owns the file.
Group: The group to which the file belongs.
Others: Everyone else on the system.
Read (r): Allows viewing the contents of a file or listing directory contents.
Write (w): Allows modifying a file or adding/removing files within a directory.
Execute (x): Allows running a file as a program or entering a directory.
Syntax:
Bash
chmod [options] mode file(s)
Common Options:
-R: Applies changes recursively to files and directories within a directory.
-v: Provides verbose output, listing changed files.
-c: Lists only files whose permissions would be changed.
Specifying Permissions:
Symbolic mode: Uses letters to represent permissions:
u for user, g for group, o for others, a for all.
+ to add permissions, - to remove permissions, = to set specific permissions.
Example: chmod u+x myscript.sh (adds execute permission for the owner)
Octal mode: Uses a three-digit number to represent permissions:
Each digit represents read, write, and execute permissions for each class (user, group, others).
Example: chmod 755 myfile.txt (gives read, write, and execute permissions to owner, read and execute to group and others)
Common Examples:
Make a script executable: chmod +x myscript.sh
Allow everyone to read a file: chmod ugo+r myfile.txt
Make a directory accessible to group members: chmod g+rx mydirectory
Remember:
Use ls -l to view current permissions before making changes.
Use sudo with chmod if you need administrative privileges.
Exercise caution, as incorrect permissions can cause security issues or prevent access to files.
Sample output:
wc command
Purpose: The wc command, short for "word count," primarily analyses files to report:
Number of lines: Each newline character marks a new line.
Number of words: Separated by spaces, tabs, or newlines.
Number of characters: Including letters, numbers, symbols, and whitespace.
Basic Usage:
Bash
wc [options] [file(s)]
[options]: Flags to customize the output.
[file(s)]: File paths to analyze (optional; defaults to standard input).
Standard Output:
By default, wc displays four values for each file:
Number of lines: How many lines are in the file.
Number of words: How many words are in the file.
Number of bytes: Total size of the file in bytes.
Filename: Name of the analysed file.
Common Options:
-c: Shows only byte count.
-l: Shows only line count.
-m: Shows only character count.
-w: Shows only word count.
-L: Displays the length of the longest line.
-t: Counts only non-empty lines.
Examples:
Count lines, words, and characters in "myfile.txt":
wc myfile.txt
Get only the word count of "article.txt":
wc -w article.txt
Count non-empty lines and longest line length in "code.py":
wc -t -L code.py
Beyond Counting:
Use wc with pipes to analyse output from other commands.
Analyse multiple files and see combined totals.
wc is a versatile tool for various tasks involving file metrics.
Remember, man wc in your terminal provides the official manual page with detailed information and more options.
Sample output