Skip to content

Linux File and Directory Management Commands

Overview

Linux provides a set of commands for managing files and directories in the filesystem. These commands allow you to create, delete, move, and manipulate files and directories. This document covers basic and commonly used file and directory management commands.

Display Current Directory

pwd

Prints the current working directory.

pwd

Example

pwd
# Output: /home/user

List Files and Directories

ls

Lists files and directories in the current directory.

ls

Example

ls
# Output: file1.txt  file2.txt  directory1

ls -l

Lists files and directories with detailed information, including permissions, ownership, and size.

ls -l

Example

ls -l
# Output:
# -rw-r--r-- 1 user user  1234 Aug  3 10:00 file1.txt
# drwxr-xr-x 2 user user  4096 Aug  3 10:00 directory1

ls -a

Lists all files, including hidden files (those starting with a dot).

ls -a

Example

ls -a
# Output: .  ..  .hiddenfile  file1.txt  directory1

Change Directory

cd <directory>

Changes the current working directory to the specified directory.

cd <directory>

Example

cd Documents
# Changes to the 'Documents' directory

cd ..

Moves up one directory level.

cd ..

Example

cd ..
# Moves up one level in the directory structure

Create a Directory

mkdir <directory>

Creates a new directory.

mkdir <directory>

Example

mkdir new_directory
# Creates a directory named 'new_directory'

mkdir -p <path>

Creates a directory and any necessary parent directories.

mkdir -p /path/to/directory

Example

mkdir -p /home/user/new_directory/subdirectory
# Creates 'new_directory' and 'subdirectory' if they do not exist

Remove a Directory

rmdir <directory>

Removes an empty directory.

rmdir <directory>

Example

rmdir old_directory
# Removes the 'old_directory' if it is empty

rm -r <directory>

Removes a directory and its contents recursively.

rm -r <directory>

Example

rm -r old_directory
# Removes 'old_directory' and all files and subdirectories within it

Create an Empty File

touch <file>

Creates an empty file or updates the timestamp of an existing file.

touch <file>

Example

touch newfile.txt
# Creates an empty file named 'newfile.txt'

Delete a File

rm <file>

Deletes a specified file.

rm <file>

Example

rm file1.txt
# Deletes the file named 'file1.txt'

Move or Rename Files and Directories

mv <source> <destination>

Moves or renames a file or directory.

mv <source> <destination>

Example

mv file1.txt /home/user/backup/
# Moves 'file1.txt' to the 'backup' directory

mv oldname.txt newname.txt

Renames a file or directory.

mv oldname.txt newname.txt

Example

mv oldname.txt newname.txt
# Renames 'oldname.txt' to 'newname.txt'

Copy Files and Directories

cp <source> <destination>

Copies a file or directory to the specified destination.

cp <source> <destination>

Example

cp file1.txt /home/user/backup/
# Copies 'file1.txt' to the 'backup' directory

cp -r <source> <destination>

Copies a directory and its contents recursively.

cp -r <source> <destination>

Example

cp -r directory1 /home/user/backup/
# Copies 'directory1' and all its contents to the 'backup' directory

View File Content

cat <file>

Displays the content of a file.

cat <file>

Example

cat file1.txt
# Displays the content of 'file1.txt'

less <file>

Allows you to view file content one page at a time.

less <file>

Example

less file1.txt
# Displays the content of 'file1.txt' one page at a time

Summary

Linux provides powerful commands for managing files and directories, including creating, deleting, moving, and viewing files. Understanding these commands allows for efficient navigation and manipulation of the filesystem. For more detailed information on each command, refer to the Linux manual pages.