Linux – File based commands

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

Bash
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

Bash
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

Bash
cp
  • Description: Copies files or directories from one location to another.
  • Usage: cp [options] source destination
  • Common Options:
  • -r or -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
Bash
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

Bash
rm
  • Description: Removes (deletes) files or directories.
  • Usage: rm [options] file
  • Common Options:
  • -r or -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

Bash
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
Bash
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
Bash
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
Bash
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
Bash
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

Bash
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., f for file, d for directory).
  • -mtime: Search by modification time (e.g., -mtime +7 for files modified more than 7 days ago).
  • -size: Search by file size (e.g., -size +100M for files larger than 100 MB). Example: find /home/user -name "*.txt"
Bash
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

Bash
chmod
  • Description: Changes file permissions.
  • Usage: chmod [options] mode file
  • Modes:
  • u for user, g for group, o for others.
  • + to add permissions, - to remove permissions, = to set permissions.
  • Numeric mode: chmod 755 file (where 7 is for user, 5 for group, and 5 for others). Example: chmod 644 file.txt (sets read/write for user, read-only for group and others)
Bash
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
Bash
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

Bash
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
Bash
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

Bash
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
Bash
gzip && gunzip
  • Description: Compresses or decompresses files using gzip.
  • Usage:
  • gzip file: Compresses file to file.gz.
  • gunzip file.gz: Decompresses file.gz to file. Example: gzip file.txt, gunzip file.txt.gz
Bash
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

Bash
ln
  • Description: Creates links between files.
  • Usage: ln [options] source link

Share
OpenLib .

OpenLib .

The Founder - OpenLib.io

You may also like...