File-related commands in Linux are essential for managing files and directories through the command line. These commands allow users to perform a variety of tasks, including creating, modifying, deleting, and searching for files. Here’s a detailed look at the most commonly used file-related commands in Linux:
1. Listing Files
ls- Description: Lists files and directories in a directory.
- Usage:
ls [options] [directory] - Common Options:
-l: Long format, displays detailed information (permissions, owner, size, modification date).-a: Includes hidden files (those starting with a dot.).-h: Human-readable sizes (e.g., KB, MB).-R: Lists directories and their contents recursively.-t: Sorts by modification time, newest first. Example:ls -lh /home/user
2. Creating Files and Directories
touch- Description: Creates an empty file or updates the timestamp of an existing file.
- Usage:
touch [options] file - Common Options:
-c: Do not create any files, only change timestamps.-t: Set a specific timestamp (format:[[CC]YY]MMDDhhmm[.ss]). Example:touch newfile.txt
mkdir
- Description: Creates a new directory.
- Usage:
mkdir [options] directory - Common Options:
-p: Creates parent directories as needed. Example:mkdir -p /home/user/newdir/subdir
3. Copying, Moving, and Renaming Files
cp- Description: Copies files or directories from one location to another.
- Usage:
cp [options] source destination - Common Options:
-ror-R: Recursively copy directories.-i: Prompt before overwrite.-u: Copy only when the source file is newer than the destination file.-p: Preserve file attributes (mode, ownership, timestamps). Example:cp -r /home/user/docs /home/user/backup
mv- Description: Moves or renames files and directories.
- Usage:
mv [options] source destination - Common Options:
-i: Prompt before overwrite.-u: Move only when the source file is newer than the destination file.-v: Verbose mode, shows what is being moved. Example:mv /home/user/oldname.txt /home/user/newname.txt
4. Deleting Files and Directories
rm- Description: Removes (deletes) files or directories.
- Usage:
rm [options] file - Common Options:
-ror-R: Recursively remove directories and their contents.-f: Force removal without prompting.-i: Prompt before every removal.-v: Verbose mode, shows what is being removed. Example:rm -rf /home/user/old_project
5. Viewing File Contents
cat- Description: Concatenates and displays the contents of files.
- Usage:
cat [options] file - Common Options:
-n: Number the lines of the output.-b: Number non-empty lines.-E: Display$at the end of each line. Example:cat -n file1.txt file2.txt
more- Description: Views file contents one page at a time.
- Usage:
more [options] file - Common Options:
-d: Display help for navigation commands.-c: Clear the screen before displaying the file. Example:more largefile.txt
less- Description: A more advanced pager that allows both forward and backward navigation.
- Usage:
less [options] file - Common Options:
-N: Display line numbers.-S: Do not wrap long lines. Example:less largefile.txt
head- Description: Displays the beginning of a file.
- Usage:
head [options] file - Common Options:
-n X: Show the first X lines (default is 10). Example:head -n 20 file.txt
tail- Description: Displays the end of a file.
- Usage:
tail [options] file - Common Options:
-n X: Show the last X lines (default is 10).-f: Follow the file as it grows (useful for log files). Example:tail -f /var/log/syslog
6. Finding Files
find- Description: Searches for files and directories based on criteria.
- Usage:
find [path] [expression] - Common Expressions:
-name: Search by name (supports wildcards).-type: Search by type (e.g.,ffor file,dfor directory).-mtime: Search by modification time (e.g.,-mtime +7for files modified more than 7 days ago).-size: Search by file size (e.g.,-size +100Mfor files larger than 100 MB). Example:find /home/user -name "*.txt"
locate- Description: Quickly finds files by name using a database.
- Usage:
locate [options] pattern - Common Options:
-i: Case-insensitive search.-r: Use regular expressions. Example:locate *.pdf
7. File Permissions
chmod- Description: Changes file permissions.
- Usage:
chmod [options] mode file - Modes:
ufor user,gfor group,ofor others.+to add permissions,-to remove permissions,=to set permissions.- Numeric mode:
chmod 755 file(where7is for user,5for group, and5for others). Example:chmod 644 file.txt(sets read/write for user, read-only for group and others)
chown- Description: Changes file owner and group.
- Usage:
chown [options] owner:group file - Common Options:
-R: Recursively change ownership of directories and their contents. Example:chown user:group file.txt
chgrp- Description: Changes the group ownership of a file or directory.
- Usage:
chgrp [options] group file - Common Options:
-R: Recursively change group ownership. Example:chgrp developers file.txt
8. Viewing and Modifying File Attributes
stat- Description: Displays detailed information about a file or directory.
- Usage:
stat [options] file - Common Output:
- Displays file size, permissions, ownership, and timestamps. Example:
stat file.txt
file- Description: Determines the type of a file.
- Usage:
file [options] file - Common Options:
-i: Show MIME type. Example:file -i file.txt
9. Archiving and Compression
tar- Description: Creates and extracts archive files.
- Usage:
tar [options] archive file - Common Options:
-c: Create a new archive.-x: Extract an archive.-f: Specify the filename of the archive.-z: Compress using gzip.-j: Compress using bzip2.-v: Verbose mode, show progress. Example:tar -czvf archive.tar.gz /home/user/dir
gzip && gunzip- Description: Compresses or decompresses files using gzip.
- Usage:
gzip file: Compressesfiletofile.gz.gunzip file.gz: Decompressesfile.gztofile. Example:gzip file.txt,gunzip file.txt.gz
zip && unzip- Description: Creates and extracts zip archive files.
- Usage:
zip archive.zip file1 file2: Create a zip archive.unzip archive.zip: Extract a zip archive. Example:zip -r archive.zip /home/user/dir,unzip archive.zip
10. Miscellaneous Commands
ln- Description: Creates links between files.
- Usage:
ln [options] source link
