Linux Common Commands
1 Linux commands for files and directory management
commands | description | commands | description |
---|---|---|---|
pwd | Print Working Directory | ls | Lists the files and directories |
cd | Change Directory | cat | Display the contents of a file |
grep | Global Regular Expression Print | cp | Copy files or directories |
touch | Create new empty files | mv | Move files or directories from one location to another. |
rm | Remove files | rmdir | Remove Directory |
2 How to use
2.1 pwd (Print Working Directory):
- Usage:
pwd
- Description: Prints the current working directory.
- Example:
$ pwd /home/user/documents
2.2 cd (Change Directory):
- Usage:
cd [directory]
- Description: Changes the current working directory to the specified directory.
- Example:
$ cd /var/log
Some additional examples for using the cd
command:
-
cd ~:
-
Description: Changes the current working directory to the user’s home directory.
-
Example:
$ cd ~
or
$ cd
-
-
cd /:
-
Description: Changes the current working directory to the root directory.
-
Example:
$ cd /
-
-
cd ..:
-
Description: Moves one directory up from the current working directory.
-
Example:
$ cd ..
-
2.3 grep (Global Regular Expression Print):
- Usage:
grep [options] pattern [files]
- Description: Searches for patterns in files and prints matching lines.
- Example:
$ grep "error" log.txt
2.4 touch:
- Usage:
touch [filename]
- Description: Creates an empty file with the specified filename.
- Example:
$ touch example.txt
2.5 rm (Remove):
- Usage:
rm [options] [files/directories]
- Description: Removes files or directories.
- Example:
$ rm example.txt
2.6 ls (List):
- Usage:
ls [options] [directory]
- Description: Lists files and directories in the current or specified directory.
- Example:
$ ls
Some examples of using the ls
command with various options:
-
ls -a (List All):
- Description: Lists all files and directories, including hidden ones (those starting with a dot).
- Example:
$ ls -a
-
ls -l (Long Format):
- Description: Lists files and directories in long format, providing detailed information such as permissions, owner, group, size, and modification time.
- Example:
$ ls -l
- ls -f (File Type):
- Description: Lists files and directories without sorting them. It also disables the implicit
-a
option. - Example:
$ ls -f
- Description: Lists files and directories without sorting them. It also disables the implicit
- ls -r (Reverse Order):
- Description: Lists files and directories in reverse order.
- Example:
$ ls -r
-
ls -R (Recursive Listing):
- Description: Lists files and directories recursively, showing the contents of subdirectories as well.
- Example:
$ ls -R
- ls -s (Size):
- Description: Lists files and directories with their sizes in blocks.
- Example:
$ ls -s
- ls -h (Human Readable):
- Description: Lists files and directories with human-readable file sizes (e.g., KB, MB, GB).
- Example:
$ ls -h
-
ls -l [filename] (Specific File):
- Description: Provides detailed information for a specific file.
- Example:
$ ls -l example.doc
2.7 cat (Concatenate):
- Usage:
cat [file]
- Description: Concatenates and displays the contents of the specified file(s).
- Example:
$ cat example.txt
2.8 cp (Copy):
- Usage:
cp [options] source destination
- Description: Copies files or directories from the source to the destination.
- Example:
$ cp example.txt backup/
2.9 mv (Move):
- Usage:
mv [options] source destination
- Description: Moves files or directories from the source to the destination. It can also rename files or directories.
- Example:
$ mv example.txt new_location/
More examples of using the mv
command with various options:
- mv -i (Interactive):
- Usage:
mv -i source destination
- Description: Prompts before overwriting existing files.
- Example:
$ mv -i file1.txt file2.txt
- Usage:
- mv -f (Force):
- Usage:
mv -f source destination
- Description: Forces the move, overwriting the destination file if it already exists without prompting. This is dangerous action!
- Example:
$ mv -f file1.txt file2.txt
- Usage:
- mv -v (Verbose):
- Usage:
mv -v source destination
- Description: Displays informative messages about the move operation.
- Example:
$ mv -v file1.txt file2.txt
- Usage:
2.10 rmdir (Remove Directory):
- Usage:
rmdir [options] directory
- Description: Removes the specified empty directory.
- Example:
$ rmdir directory_name
3 Linux commands for disk space operation
commands | description |
---|---|
mount | Attaches a filesystem to the directory tree. |
umount | Detaches a filesystem from the directory tree. |
df | Displays disk space usage for mounted filesystems. |
du | Estimates disk usage for directories and files. |
fsck | Checks and repairs filesystem inconsistencies. |
4 How to use
Certainly! Here’s a more detailed explanation of how to use each command along with examples:
4.1 mount:
- Usage:
mount [options] device directory
- Description: Mounts a filesystem located on a device (such as a hard drive partition or a network share) to a specified directory in the filesystem hierarchy.
- Example:
$ sudo mount /dev/sdb1 /mnt/usb
4.2 umount:
- Usage:
umount [options] directory
- Description: Unmounts a previously mounted filesystem from the directory tree, ensuring that no processes are using the filesystem.
- Example:
$ sudo umount /mnt/usb
4.3 df:
- Usage:
df [options] [directory]
- Description: Displays information about disk space usage for all mounted filesystems or the filesystem containing the specified directory.
- Example:
$ df -h
4.4 du:
- Usage:
du [options] [directories/files]
- Description: Estimates disk usage for the specified directories and files, showing the total size occupied by each.
- Example:
$ du -sh /home/user
4.5 fsck:
- Usage:
fsck [options] device
- Description: Checks and repairs filesystem inconsistencies on a disk device, ensuring the integrity of the filesystem.
- Example:
$ sudo fsck /dev/sda1
Enjoy Reading This Article?
Here are some more articles you might like to read next: