Basic File Management Commands - Linux & Unix Guide

Master basic file management commands in Linux and Unix. Learn to list, view, copy, move, delete, and link files and directories with practical examples.

Basic File Management Commands

9. Basic File Management Commands

Effective file management is fundamental to navigating and operating within any command-line environment, especially Linux and Unix-like systems. This section covers essential commands for listing, viewing, manipulating, and organizing files and directories.

9.1. Listing Directory Contents

The ls command is used to list files and directories. It offers various options to customize the output.

Command Description
ls List the contents of the current directory.
ls * List contents of the current directory along with subdirectories.
ls -l List contents in a long format, showing owner, permissions, date, and size.
ls -a List all files, including hidden files (those starting with a dot).
ls -t List files sorted by last modified date in descending order.
ls -rt List files sorted by last modified date in ascending order.
ls -R Recursively list files of the current directory and all subdirectories.
ls /path/to/directory List files in the specified directory.

9.2. Displaying File Contents

These commands allow you to view the content of files, which is crucial for inspecting configuration files, logs, or source code.

Command Description
cat demo.txt Display the entire contents of a file. Best for small files.
head demo.txt Display the first few lines of a file (default is 10).
tail demo.txt Display the last few lines of a file (default is 10).
tail -f demo.txt Follow the file, displaying new lines as they are appended. Useful for monitoring logs.
less demo.txt Display file contents one screen at a time, allowing navigation.
less -p "pattern" demo.txt Display file contents, starting from the first occurrence of "pattern".
strings -a binaryfile Extract and print all printable character sequences from a file, useful for examining binary files.
diff file1 file2 Show the differences between two files line by line.
comm file1 file2 Compare two sorted files line by line, showing lines unique to file1, unique to file2, and common to both.

9.3. File Handling Operations

This section covers the core commands for managing files and directories: copying, moving, renaming, and deleting.

9.3.1. Copying Files and Directories

The cp command is used to duplicate files and directories.

Command Description
cp file1 file2 Copy file1 to a new file named file2.
cp file1 file2 directory1 Copy file1 and file2 into directory1.
cp -R directory1 directory2 Recursively copy the contents of directory1 to directory2.
cp *.txt directory1 Copy all files ending with .txt into directory1.

9.3.2. Moving or Renaming Files and Directories

The mv command is used for both moving files/directories and renaming them.

Command Description
mv file1 file2 Rename file1 to file2.
mv file1 directory1/ Move file1 into directory1.
mv *.jpg directory1/ Move all files ending with .jpg into directory1.

9.3.3. Deleting Files and Directories

The rm command is used to remove files and directories. Use with caution, as deleted items are typically not recoverable.

Command Description
rm file1 Remove (delete) file1.
rm file1 file2 Remove both file1 and file2.
rm *.png Remove all files ending with .png.
rm -d emptyDirectory Remove an empty directory.
rm -r directory1 Recursively remove directory1 and all its contents (files and subdirectories).

9.3.4. File Linking

Linking allows you to create references to files or directories. There are two main types of links:

  • Hard Link: An additional name for an existing file. It points directly to the file's inode. If the original file is deleted, the hard link still provides access to the data.
  • Soft Link (Symbolic Link): An indirect pointer to a file or directory. It stores the path to the original file. If the original file is deleted, the soft link becomes broken.
Command Description
ln file1 link1 Create a hard link named link1 to file1.
ln -s file1 link1 Create a symbolic (soft) link named link1 to file1.

9.3.5. Changing Directory

The cd command is used to navigate between directories in the file system.

Command Description
cd Change to the home directory.
cd ~ Change to the home directory.
cd - Change to the previously current directory.
cd .. Change to the parent directory (one level up).
cd / Change to the root directory of the file system.
cd ~/dir1/dir2 Change to a directory relative to the home directory.

9.3.6. Displaying Disk Usage

The du command estimates and displays the disk space used by files and directories.

Command Description
du Estimate and display disk space usage for files and directories.
du -h * Display disk usage in a human-readable format (e.g., KB, MB, GB) for all items in the current directory.

9.3.7. File Ownership and Permissions

Managing file permissions and ownership is critical for security and access control.

Command Description
chmod Change file mode bits (permissions).
chown Change file owner and group.

Understanding and utilizing these basic file management commands will significantly enhance your productivity and control when working with the command line.