2 ‐ File Operation - CloudScope/DevOpsWithCloudScope GitHub Wiki
In Linux, file operations are commonly performed using various command-line utilities. Here is a list of essential file operation commands, along with explanations of their functionality:
ls - List Directory Contents
Usage: ls [options] [directory]
Explanation: Lists the files and directories within the specified directory. Without options, it shows the contents of the current directory.
Example:
$ ls -l (detailed list with permissions, owner, etc.)
cd - Change Directory
Usage: cd [directory]
Explanation: Changes the current working directory to the specified directory.
Example:
$ cd /home/user/Documents
pwd - Print Working Directory
Usage: pwd
Explanation: Displays the full path of the current working directory.
Example:
$ pwd
touch - Create an Empty File
Usage: touch [filename]
Explanation: Creates an empty file or updates the timestamp of an existing file.
Example:
$ touch newfile.txt
cat - Concatenate and Display File Contents
Usage: cat [filename]
Explanation: Displays the content of a file or concatenates multiple files.
Example:
$ cat file1.txt file2.txt
cp - Copy Files or Directories
Usage: cp [options] source destination
Explanation: Copies files or directories from the source to the destination.
Example:
$ cp file.txt /backup/file.txt (copy file to the backup directory)
mv - Move or Rename Files or Directories
Usage: mv [source] [destination]
Explanation: Moves or renames files or directories.
Example:
$ mv oldname.txt newname.txt (renames the file)
rm - Remove Files or Directories
Usage: rm [options] [file or directory]
Explanation: Deletes files or directories.
Example:
$ rm file.txt (removes the specified file)
Important: rm -r directory (removes a directory and its contents)
mkdir - Make Directories
Usage: mkdir [options] [directory name]
Explanation: Creates a new directory.
Example:
$ mkdir newdir
rmdir - Remove Empty Directories
Usage: rmdir [directory]
Explanation: Deletes an empty directory.
Example:
$ rmdir emptydir
find - Search for Files and Directories
Usage: find [path] [options] [expression]
Explanation: Searches for files and directories based on specified criteria.
Example:
$ find /home/user -name "*.txt" (finds all .txt files in the user's home directory)
locate - Find Files by Name
Usage: locate [filename]
Explanation: Quickly searches for files by name using a pre-built database.
Example:
$ locate file.txt
grep - Search Inside Files
Usage: grep [options] pattern [file]
Explanation: Searches for a specified pattern within files.
Example:
$ grep "hello" file.txt (finds the word "hello" in file.txt)
head - Display the Beginning of a File
Usage: head [options] [file]
Explanation: Displays the first few lines of a file.
Example:
$ head -n 10 file.txt (displays the first 10 lines)
tail - Display the End of a File
Usage: tail [options] [file]
Explanation: Displays the last few lines of a file.
Example:
$ tail -n 10 file.txt (displays the last 10 lines)
chmod - Change File Permissions
Usage: chmod [options] mode file
Explanation: Modifies the file or directory permissions.
Example:
$ chmod 755 script.sh (gives execute permissions)
chown - Change File Owner and Group
Usage: chown [options] owner[:group] file
Explanation: Changes the ownership of a file or directory.
Example:
$ chown user:group file.txt
ln - Create Links
Usage: ln [options] target link_name
Explanation: Creates hard or symbolic links between files.
Example:
$ ln -s /path/to/file linkname (creates a symbolic link)
stat - Display File or Filesystem Status
Usage: stat [file]
Explanation: Provides detailed information about a file or file system.
Example:
$ stat file.txt
du - Estimate File Space Usage
Usage: du [options] [directory or file]
Explanation: Displays the disk usage of files and directories.
Example:
$ du -sh /home/user (shows the total disk usage of the user’s home directory)
df - Report File System Disk Space Usage
Usage: df [options]
Explanation: Displays the available and used disk space on mounted filesystems.
Example:
$ df -h (human-readable format)
tar - Archive Files
Usage: tar [options] archive_name files
Explanation: Archives multiple files into a single file or extracts files from an archive.
Example:
$ tar -cvf archive.tar file1.txt file2.txt (creates an archive)
gzip / gunzip - Compress/Decompress Files
Usage: gzip [file] / gunzip [file.gz]
Explanation: Compresses a file or decompresses a .gz file.
Example:
$ gzip file.txt (compresses the file) / gunzip file.txt.gz (decompresses)
zip / unzip - Compress/Decompress Files into/from Zip Archive
Usage: zip [options] archive.zip files / unzip archive.zip
Explanation: Compresses files into a .zip archive or extracts files from a .zip archive.
Example:
$ zip archive.zip file1.txt file2.txt / unzip archive.zip
scp - Secure Copy
Usage: scp [options] source destination
Explanation: Securely copies files between hosts over a network.
Example:
$ scp file.txt user@remote:/path/to/destination
rsync - Remote Sync
Usage: rsync [options] source destination
Explanation: Synchronizes files and directories between two locations, either locally or remotely.
Example:
$ rsync -avz /path/to/source /path/to/destination
file - Determine File Type
Usage: file [filename]
Explanation: Determines the type of a file (e.g., text, binary, directory, etc.).
Example:
$ file file.txt
find - To find a file or directory
Usage: find /home -name "*.txt"
grep - search and print lines with required parameter
Usage: grep "error" /tmp/error.log
$ grep -i "error" /tmp/error.log
-i
: case-insensitive
-c
: Count occurrences of "error" in a file
$ grep -C 2 "TODO" file.txt
-C
: Show lines with "TODO" and include 2 lines of context before and after:
$ grep -l "TODO" *
-l
: Find all files in the current directory that contain "TODO":
Soft link:
A soft link (or symbolic link) is a special type of file that contains a reference (path) to another file or directory. It acts as a shortcut or alias.
Characteristics:
-
Different Inode: Soft links have their own inodes and point to the path of the target file. They do not share the inode with the target file.
-
File System Independent: Soft links can reference files or directories on different file systems or partitions.
-
Broken Links: If the target file is deleted or moved, the soft link becomes a "dangling" link and will no longer work. It simply points to a non-existent location.
-
Can Link Directories: soft links can point to directories, which is useful for creating shortcuts to directories.
$ ln -s target_file soft_link
$ ln -s /etc/apache2/sites-available/apach2.conf /etc/apache2/sites-enable/apach2.conf
Hard link:
A hard link is an additional directory entry for an existing file. It points directly to the inode (the data structure storing the file's metadata and content) of the original file.
Characteristics:
-
Same Inode: Both the original file and the hard link share the same inode number. This means they are essentially the same file.
-
File System Bound: Hard links can only be created within the same file system. You cannot create a hard link to a file on a different file system.
-
Indistinguishable: There is no "original" file; both the hard link and the original file are equally valid. Deleting one does not affect the other; only when all hard links to an inode are deleted is the inode and its data removed.
-
Cannot Link Directories: Hard links generally cannot be created for directories to prevent looping and complexity in directory structures.
$ ln existing_file hard_link
Summary
Hard Links: Share the same inode, cannot span file systems, and cannot link directories. Deleting one link does not delete the file until all links are removed.
Soft Links: Have their own inode, can span file systems, and can link directories. They become broken if the target is deleted or moved.