Linux : Essential Commands - Part 1 (Day 2)
Bhupesh Patil ?
DevSecOps Engineer ??? | 2x Microsoft Azure ? 1x OCI ?? | Go ? Docker ? Kubernetes ? CI/CD ? Security ? Obeservabillity ? Terraform ?????? ||
Working with Files & Directories :-
ls - List Directory Contents
- Usage: ls [options] [file...]
- Options:
-a : List all entries including those starting with a dot ..
-l : Use a long listing format.
-h : Human readable form
- Example: ls -la /home/user lists all files, including hidden ones, in the /home/user directory with detailed information.
pwd - Print Working Directory
- Usage: pwd [options]
- Options: This command typically does not have many options.
- Example: pwd displays the current directory you are in.
cd - Change Directory
- Usage: cd [directory]
- Options: No options. If no directory is given, it defaults to the home directory.
- Example: cd /var/www changes the current directory to /var/www.
touch - Create Empty Files or Update Timestamps
- Usage: touch [options] file...
- Options:
-a : Change only the access time.
-m : Change only the modification time.
- Example: touch newfile.txt creates a new empty file named newfile.txt.
mkdir - Make Directories
- Usage: mkdir [options] directory...
- Options:
-p : Make parent directories as needed.
- Example: mkdir -p /path/to/new/directory creates the directory along with any necessary parent directories.
cp - Copy Files and Directories
- Options:
-r : Copy directories recursively.
- Example: cp -r /source/directory /destination/directory copies the source directory to the destination directory.
mv - Move (Rename) Files
- Usage: mv [options] source destination
- Options: No specific options for basic usage.
- Example: mv oldname.txt newname.txt renames oldname.txt to newname.txt.
rm - Remove Files or Directories
- Usage: rm [options] file...
- Options:
-r : Remove directories and their contents recursively.
-f : Force removal without confirmation.
- Example: rm -rf /path/to/directory removes the directory and all its contents without asking for confirmation.
Create and manage Hard links & Soft links :-
- Description: A hard link is essentially another name for an existing file on the same filesystem. It points directly to the file's inode and has no differentiation between the original file and the hard link.
- Usage: ln [options] target link_name
- Options: Typically, no options are needed for creating a hard link.
- Example: ln original.txt hardlink.txt creates a hard link named hardlink.txt pointing to original.txt.
- Description: A soft link, or symbolic link, is a special type of file that points to another file or directory. It's a reference, similar to a shortcut, and can span across different filesystems.
- Usage: ln -s [options] target link_name
- Options:
-s: Create a symbolic link.
- Example: ln -s /path/to/original /path/to/link creates a symbolic link named link which points to /path/to/original.
- Persistence: Deleting the original file will not affect hard links, but it will break soft links.
- Cross-Filesystem: Hard links cannot span across different filesystems, whereas soft links can.
- Directories: Hard links to directories are typically not allowed, but soft links can link to directories.
- Visibility: Running ls -l will show hard links as if they were regular files, while soft links will be displayed with an arrow (`->`) pointing to the target.
Note: Always use caution when creating links, especially when working with system files or directories, to avoid data loss or system issues.
List , Set & Change file permissions :
ls -l
- Description: The ls -l command lists files in long format, which includes file permissions.
- Example: ls -l file.txt will show the permissions of file.txt in the first column.
- Format: -rwxrwxrwx
- The first character indicates the type of file (`-` for regular file, d for directory).
- The next three characters (`rwx`) are permissions for the owner (read, write, execute).
- The following three are permissions for the group.
- The last three are permissions for others.
chmod
- Usage: chmod [options] mode file...
- Options:
-R : Change files and directories recursively.
领英推荐
- Mode:
- Numeric method: chmod 755 file.txt
- Symbolic method: chmod u+rwx,g+rx,o+rx file.txt
- Description: chmod changes the file mode bits. The numeric method uses octal numbers to represent permissions, while the symbolic method uses letters (`u` for user, g for group, o for others) and symbols (`+` to add, - to remove, = to set exactly).
umask
- Usage: umask [mask]
- Description: umask sets the default file creation permissions. The mask is subtracted from the default permissions (usually 666 for files and 777 for directories).
- Example: umask 022 sets the default permissions so that new files will have 644 and new directories will have 755.
Note: Modifying file permissions can affect system security and functionality. Always ensure you understand the implications of changing permissions, especially on system files or directories.
Search for Files :
find - Search for Files in a Directory Hierarchy
- Usage: find [path...] [expression]
- Description: The find command is used to search for files and directories within a specified directory hierarchy based on various criteria such as name, type, size, and more.
- `-name`: Search for files by name.
- Example: find /home/user -name "*.txt" finds all .txt files in /home/user.
- `-type`: Search for files of a particular type.
- Example: find / -type d finds all directories in the root filesystem.
- `-size`: Search for files of a specific size.
- Example: find / -size +50M finds files larger than 50 megabytes.
- `-perm`: Search for files with specific permissions.
- Example: find / -perm 644 finds files with 644 permissions.
- `-user`: Search for files owned by a specific user.
- Example: find /home -user alice finds files owned by user alice.
- `-mtime`, `-atime`, `-ctime`: Search for files modified, accessed, or changed within a given time frame.
- Example: find / -mtime -7 finds files modified in the last 7 days.
- Logical AND: find / -name "*.php" -type f (implicit AND between expressions)
- Logical OR: find / \( -name "*.php" -o -name "*.html" \) (finds .php or .html files)
- Negation: find / ! -name "*.txt" (finds all files not ending with .txt)
exec : Execute a command on each found file.
- Example: find / -type f -exec chmod 644 {} \; changes the permissions of all found files to 644.
Note: The find command is very powerful and can affect many files. Use it with caution, especially when combined with the -exec option. Always double-check the criteria and commands before executing them.
Compare & Manipulate file content :
cat - Concatenate and Display Files
- Usage: cat [options] [file...]
- Description: The cat command reads files sequentially and writes them to standard output.
- Options:
-n: Number all output lines.
- Example: cat file.txt displays the contents of file.txt.
tac - Concatenate and Display Files in Reverse
- Usage: tac [file...]
- Description: The tac command is like cat, but it displays the contents in reverse (last line first).
- Example: tac file.txt displays the contents of file.txt starting with the last line.
head - Output the First Part of Files
- Usage: head [options] [file...]
- Description: The head command outputs the first part of files.
- Options:
-n: Specify the number of lines to display (default is 10).
- Example: head -n 5 file.txt displays the first 5 lines of file.txt.
tail - Output the Last Part of Files
- Usage: tail [options] [file...]
- Description: The tail command outputs the last part of files.
- Options:
-n: Specify the number of lines to display (default is 10).
-f: Follow the contents of a file in real-time as it grows.
- Example: tail -n 5 file.txt displays the last 5 lines of file.txt.
Trasforming or Replace text :
sed - Stream Editor for Filtering and Transforming Text
- Usage: sed [options] script [input_file...]
- Description: The sed command is a stream editor that performs basic text transformations on an input stream (a file or input from a pipeline).
- Options:
-e: Add the script to the commands to be executed.
-i: Edit files in-place (makes changes to the original file).
- Example: sed 's/original/replacement/' file.txt replaces the first occurrence of 'original' with 'replacement' in each line of file.txt.
Note: These commands are powerful text processing tools. Use them with caution, especially sed with the -i option, as it can alter the original file content.