Loading...

60 Linux Command | Part-1 | Forum

Topic location: Forum home » General » General Chat
Admin
Admin Yesterday, 14:16

1. ls command

The ls command displays files and directories within a specified location.


Syntax:


ls [options] [directory_or_path]

Common options:

• -a → shows hidden files, including those starting with a dot (.)

• -l → displays a detailed list with permissions, ownership, size, and timestamps


Example:


ls -Ra /path/to/dir

Lists all files and subdirectories recursively inside /path/to/dir, including hidden files, allowing you to inspect the full directory structure.


2. pwd command

The pwd command prints the full path of the current working directory.


Syntax:


pwd [options]

Common options:

• -L → prints the logical path using symbolic links or environment shortcuts

• -P → prints the physical path by resolving all symbolic links


Example:


pwd -P

Displays the exact directory path without symbolic links, helping verify the actual location of the current working directory.


3. cd command

The cd command changes the current working directory to a specified location.


Syntax:


cd [path_or_directory]

Common options: (no options available for this command)


Common shortcuts:

• cd → moves to the current user’s home directory

• cd .. → moves one directory up

• cd - → returns to the previous directory


Example:


cd /var/www/html

Changes the current directory to /var/www/html, allowing you to access and manage files in that location.


4. mkdir command

The mkdir command creates one or more directories at a specified location.


Syntax:


mkdir [options] directory_name1 directory_name2

Common options:

• -m → sets custom permissions for the new directory (e.g., -m 755)

• -p → creates parent directories as needed and avoids errors if they already exist


Example:


mkdir -p /path/to/target_folder/new_folder

Creates the directory /path/to/target_folder/new_folder, including any missing parent directories, allowing you to build nested folder structures in one command.


5. rmdir command

The mkdir command creates one or more directories at a specified location.


Syntax:


mkdir [options] directory_name1 directory_name2

Common options:

• -m → sets custom permissions for the new directory (e.g., -m 755)

• -p → creates parent directories as needed and avoids errors if they already exist


Example:


mkdir -p /path/to/target_folder/new_folder

Creates the directory /path/to/target_folder/new_folder, including any missing parent directories, allowing you to build nested folder structures in one command.


6. rm command

The rm command removes files or directories from the file system.


Syntax:


rm [options] file1 file2

Common options:

• -r → removes directories and their contents recursively

• -f → forces deletion without confirmation prompts

• -i → prompts for confirmation before each removal


Example:


rm -rf folder-name

Deletes the directory folder-name and all its contents recursively without confirmation, which is useful for cleanup tasks but requires caution to avoid accidental data loss.


7. cp command

The cp command copies files or directories from one location to another.


Syntax:


cp [options] source_file target_path

Common options:

• -R → copies directories and their contents recursively

• -i → prompts before overwriting existing files

• -v → displays detailed output of the copy process


Example:


cp -R /path/to/folder /target/path/to/folder_copy

Copies the directory /path/to/folder and all its contents to /target/path/to/folder_copy, preserving the structure for backup or duplication tasks.


8. mv command

The mv command moves or renames files and directories.


Syntax:


mv [options] source target

Common options:

• -i → prompts before overwriting existing files

• -v → displays detailed output of the move operation

• -n → prevents overwriting existing files


Example:


mv /original/path/file1.txt /new/file/directory/

Moves file1.txt from /original/path/ to /new/file/directory/, allowing you to reorganize files or relocate them to a different folder.


9. touch command

The touch command creates a new empty file or updates the timestamp of an existing file.


Syntax:


touch [options] [path_and_file_name]

Common options:

• -c → avoids creating a file if it does not exist

• -t → sets a specific timestamp for the file

• -a → updates only the access time


Example:


touch file.txt

Creates an empty file named file.txt in the current directory, allowing you to quickly generate placeholder or configuration files.


10. file command

The file command identifies the type of a file based on its content.


Syntax:


file [file_name]

Common options:

• -k → displays additional information about the file type

• -i → shows the MIME type of the file

• -L → follows symbolic links and reports the actual file type


Example:


file hostinger_sym.txt

Displays the actual file type that hostinger_sym.txt points to, which helps verify symbolic links or detect unknown file formats.


11. zip and unzip commands

The zip command compresses files or directories into a ZIP archive, while the unzip command extracts files from a ZIP archive.


Syntax:


zip [options] zip_file_name file1 file2

unzip [options] zip_file_name

Common options:

• -r (zip) → compresses directories recursively

• -e (zip) → encrypts the archive with a password

• -l (unzip) → lists contents of the archive without extracting

• -d (unzip) → extracts files to a specified directory


Example:


zip -r archive.zip /path/to/folder && unzip archive.zip

Creates a compressed archive archive.zip from /path/to/folder and then extracts its contents into the current directory, allowing you to bundle and restore files efficiently.


12. tar command

The tar command creates, extracts, and manages archive files that bundle multiple files or directories.


Syntax:


tar [options] tar_file_name file1 file2

Common options:

• -c → creates a new archive

• -x → extracts files from an archive

• -f → specifies the archive file name

• -z → compresses or decompresses using gzip


Example:


tar -czf archive.tar.gz file1.txt file2.txt

Creates a compressed archive archive.tar.gz containing file1.txt and file2.txt, allowing you to bundle and reduce file size in a single command.


13. nano, vi, and jed command

The nano, vi, and jed commands open and edit text files directly in the terminal.


Syntax:


nano file_name

vi file_name

jed file_name

Common options: (no commonly used options required for basic file editing)


Example:


nano file.txt

Opens file.txt in the Nano text editor, allowing you to create or modify file content directly from the terminal.


14. cat command

The cat command displays, combines, or creates files by reading and writing file content.


Syntax:


cat [options] file_name

Common options:

• -n → displays line numbers alongside file content

• -b → displays line numbers for non-empty lines only

• -s → suppresses repeated empty lines


Example:


cat file1.txt file2.txt > target.txt

Combines the contents of file1.txt and file2.txt into target.txt, allowing you to merge multiple files into a single output file.


15. grep command

The grep command searches for specific patterns or keywords within files or command output.


Syntax:


grep [options] keyword [file]

Common options:

• -i → performs a case-insensitive search

• -r → searches recursively through directories

• -n → displays line numbers with matching results


Example:


ls | grep "file.txt"

Filters the output of the ls command to show only entries that match file.txt, allowing you to quickly locate specific items in a list.


16. sed command

The sed command searches, replaces, and transforms text within files or input streams.


Syntax:


sed [options] 's/pattern/replacement/' file

Common options:

• -i → edits files in place without creating a new output

• -n → suppresses automatic output (used with explicit print commands)

• -e → allows multiple editing expressions


Example:


sed 's/red/blue/' colors.txt

Replaces the first occurrence of red with blue in each line of colors.txt, allowing quick text modifications without opening a file manually.


17. head command

The head command displays the first lines or bytes of a file or command output.


Syntax:


head [options] file_name

Common options:

• -n → specifies the number of lines to display

• -c → specifies the number of bytes to display


Example:


head -n 5 file.txt

Displays the first five lines of file.txt, allowing you to quickly preview the beginning of a file without opening it fully.


18. tail command

The tail command displays the last lines or bytes of a file or command output.


Syntax:


tail [options] file_name

Common options:

• -n → specifies the number of lines to display

• -c → specifies the number of bytes to display

• -f → follows the file in real time as new lines are added


Example:


ping -c 10 8.8.8.8 | tail -n 5

Displays the last five lines of the ping command output, allowing you to focus on the most recent results or summaries.


19. awk command

The awk command processes and analyzes text by applying patterns and actions to structured data.


Syntax:


awk 'pattern {action}' input_file

Common options:

• -F → specifies a custom field separator

• -v → assigns variables for use within the command

• -f → reads the awk program from a file


Example:


awk -F':' '{ total += $2; students[$1] = $2 } END { average = total / length(students); for (student in students) if (students[student] > average) print student }' score.txt

Calculates the average value from score.txt and prints the names of entries with values above the average, allowing you to perform data analysis directly in the terminal.


20. sort command

The sort command arranges lines of text in a file or input stream in a specified order.


Syntax:


sort [options] [file_name]

Common options:

• -r → sorts in reverse order

• -n → sorts numerically instead of alphabetically

• -k → sorts based on a specific column or field


Example:


sort -n file.txt

Sorts the contents of file.txt numerically and prints the result to the terminal without modifying the original file.


21. cut command

The cut command extracts specific sections of text from each line of a file or input stream.


Syntax:


cut [options] file

Common options:

• -f → selects specific fields (columns)

• -d → specifies a delimiter to separate fields

• -c → extracts specific character positions

• -b → extracts specific byte ranges


Example:


cut -d',' -f3-5 list.txt

Extracts the third to fifth fields from each line in list.txt using a comma as the delimiter, allowing you to isolate structured data from files like CSVs.


22. diff command

The diff command compares two files and displays the differences between them.


Syntax:


diff [options] file_name1 file_name2

Common options:

• -c → displays differences in context format with surrounding lines

• -u → shows unified output format (commonly used for patches)

• -i → ignores differences in letter case


Example:


diff -c 1.txt 2.txt

Displays the differences between 1.txt and 2.txt with surrounding context, making it easier to understand changes between file versions.


23. tee command

The tee command writes command output to both the terminal and a file simultaneously.


Syntax:


command | tee [options] file_name

Common options:

• -a → appends output to the file instead of overwriting it

• -i → ignores interrupt signals (useful in pipelines)


Example:


ping 8.8.8.8 | tee -a test_network.txt

Appends the output of the ping command to test_network.txt while displaying it in the terminal, allowing you to monitor and save results at the same time.


24. locate command

The locate command searches for files by name using a prebuilt system database.


Syntax:


locate [options] keyword

Common options:

• -i → performs a case-insensitive search

• -r → searches using a regular expression

• -n → limits the number of results displayed


Example:


locate -i filename.txt

Searches for all files matching filename.txt regardless of case, returning results quickly based on the system’s file index.


25. find command

The find command searches for files and directories in a specified path based on conditions.


Syntax:


find [path] [options] expression

Common options:

• -name → searches for items by name

• -type f → limits results to files

• -type d → limits results to directories


Example:


find /path/to/folder -type f -name "file.txt"

Searches for a file named file.txt within /path/to/folder, returning matches in real time based on the specified criteria.


26. sudo command

The sudo command runs a command with elevated (administrator) privileges.


Syntax:


sudo [options] command

Common options:

• -u → runs the command as a specified user

• -i → starts a shell with root privileges

• -l → lists allowed commands for the current user


Example:


sudo nano file.txt

Opens file.txt in the Nano editor with administrator privileges, allowing you to modify system-level files that require elevated access.


27. su and whoami commands

The su command switches the current user to another user account, while the whoami command displays the currently logged-in user.


Syntax:


su [options] [username]

whoami

Common options:

• - → starts a login shell for the target user

• -c → runs a single command as the specified user


Example:


su - root

Switches to the root user and starts a login shell, allowing you to execute commands with full system privileges.


28. chmod command

The chmod command changes file and directory permissions for users, groups, and others.


Syntax:


chmod [options] permissions file_or_directory

Common options:

• -R → applies changes recursively to directories and their contents

• -v → displays a message for each processed file

• -c → reports only when a change is made


Example:


chmod 744 file1.txt

Sets permissions for file1.txt so the owner can read, write, and execute, while others can only read, allowing controlled access to the file.


29. chown command

The chown command changes the ownership of files and directories.


Syntax:


chown [options] owner:group file1 file2

Common options:

• -R → applies ownership changes recursively

• -v → displays a message for each processed file

• -c → reports only when a change is made


Example:


chown admin-vps:developers file1.txt

Sets admin-vps as the owner and developers as the group for file1.txt, allowing proper access control and file management.


30. useradd, passwd, and userdel command

The useradd command creates a new user account, the passwd command sets or updates a user’s password, and the userdel command removes a user account.


Syntax:


useradd [options] username

passwd username

userdel [options] username

Common options:

• -m (useradd) → creates a home directory for the user

• -r (userdel) → removes the user’s home directory and files

• -l (passwd) → locks a user account


Example:


sudo useradd -m newuser && sudo passwd newuser

Creates a new user newuser with a home directory and sets a password, allowing the account to log in and use the system.


31. df command

The df command displays disk space usage for file systems.


Syntax:


df [options] [file_system]

Common options:

• -h → shows output in human-readable format (KB, MB, GB)

• -T → displays the file system type

• -a → includes all file systems, including empty ones


Example:


df -h

Displays disk usage for all mounted file systems in a human-readable format, helping you quickly assess available and used storage space.


32. du command

The du command checks the size of a directory and its content.


Syntax:


du [directory]

The command will check your working directory if you don’t specify a path or folder. By default, it breaks down each subfolder’s disk usage, but you can add the -s option to summarize the total usage in one output.


You can also use the -M option to change the information from KB to MB.



The Forum post is edited by Admin Yesterday, 14:19