Basic Commands Linux-1
Basic Commands Linux-1
A. FILE OPERATIONAL
You can combine multiple options to achieve the desired output. For example, `ls -al` will list all files and directories in the current
directory in long format, including hidden ones.
Remember to replace `<directory>` with the actual directory path you want to list.
These commands provide various options to customize the listing of files and directories in Linux. Experiment with them to find the
format that suits your needs.
2. Change Directory:
Here is a list of Linux commands to change directories:
Note: In Linux, the tilde (`~`) represents the home directory of the current user.
Examples:
- To change to the `/var/log` directory, use the command: `cd /var/log`
- To change to the previous directory, use the command: `cd -`
These commands allow you to navigate through the directory structure in Linux. Remember to provide the correct directory name or
path for successful navigation.
4. Create Directory:
- `mkdir <directory>`: Create a new directory.
5. Remove Files and Directories:
- `rm <file>`: Remove a file.
- `rm -r <directory>`: Remove a directory and its contents recursively.
- `rm -f <file>`: Forcefully remove a file without prompting for confirmation.
- `rm -rf <directory>`: Forcefully remove a directory and its contents without prompting for confirmation.
8. File Permissions:
- `chmod <permissions> <file>`: Change the permissions of a file.
- `chown <user> <file>`: Change the owner of a file.
- `chgrp <group> <file>`: Change the group ownership of a file.
Here is a list of Linux commands for managing file permissions:
Examples:
- To set read, write, and execute permissions for the owner, and read and execute permissions for the group and others, on a file
named `script.sh`, use the command: `chmod 755 script.sh`
- To change the owner of a file named `file.txt` to a user named `user1`, use the command: `chown user1 file.txt`
- To change the group ownership of a file named `file.txt` to a group named `group1`, use the command: `chgrp group1 file.txt`
These commands are used to manage file permissions, ownership, and group ownership in Linux. Ensure that you have appropriate
permissions to modify the files and directories.
1. `tar <options> <archive-file> <files>`: Create a tarball or extract files from a tarball.
- `<options>`: Various options can be used with the `tar` command to specify the operation, compression, and more.
- `<archive-file>`: The name of the tarball archive file to create or extract from.
- `<files>`: The files or directories to include in the tarball or extract from it.
These are some commonly used Linux commands for file operations. Each command has various options and arguments to perform
specific tasks. You can refer to the respective command's man pages for detailed information on their usage and options by typing
`man <command>` in the terminal.
1. File Viewing:
Here is a list of Linux commands for file viewing:
1. `cat <file>`: Display the content of a file. It prints the entire file to the terminal.
Example: `cat myfile.txt`
2. `more <file>`: View the content of a file page by page. Press the Spacebar to scroll to the next page and press Q to exit.
Example: `more myfile.txt`
3. `less <file>`: View the content of a file with backward navigation. It provides more advanced features compared to `more`, such as
searching and scrolling through the file. Press the Spacebar to scroll forward, and press Q to exit.
Example: `less myfile.txt`
4. `head <file>`: Display the first few lines of a file. By default, it shows the first 10 lines, but you can specify a different number using
the `-n` option.
Example: `head -n 5 myfile.txt` (Displays the first 5 lines)
5. `tail <file>`: Display the last few lines of a file. By default, it shows the last 10 lines, but you can specify a different number using
the `-n` option.
Example: `tail -n 5 myfile.txt` (Displays the last 5 lines)
6. `tail -f <file>`: Monitor the changes in a file in real-time. It continuously displays the appended lines as they are added to the file.
Example: `tail -f myfile.txt`
7. `grep <pattern> <file>`: Search for a specific pattern within a file. It displays the lines containing the pattern.
Example: `grep "error" myfile.log`
These commands provide different ways to view the content of files in Linux. Choose the appropriate command based on your
specific requirements and the desired output.
2. File Editing:
Here is a list of Linux commands for file editing:
1. `vi <file>`: Open a file in the Vi text editor. Vi has multiple modes for editing. Press I to enter insert mode, Esc to exit insert mode,
:wq to save and exit, and :q! to exit without saving.
Example: `vi myfile.txt`
2. `nano <file>`: Open a file in the Nano text editor. Nano provides a user-friendly and straightforward interface. Use the arrow keys to
navigate, and press Ctrl + O to save and Ctrl + X to exit.
Example: `nano myfile.txt`
3. `emacs <file>`: Open a file in the Emacs text editor. Emacs is a powerful and extensible text editor with its own set of commands
and keybindings.
Example: `emacs myfile.txt`
4. `sed -i 's/<pattern>/<replacement>/g' <file>`: Replace occurrences of a pattern with a replacement string in a file. The `-i` option
modifies the file in-place.
Example: `sed -i 's/foo/bar/g' myfile.txt` (Replaces all occurrences of "foo" with "bar" in myfile.txt)
5. `awk '<pattern> {<action>}' <file>`: Perform actions on specific patterns within a file using the AWK scripting language.
Example: `awk '/pattern/ {print $1}' myfile.txt` (Prints the first field of lines containing "pattern" in myfile.txt)
These commands offer different options for editing files in Linux. Choose the one that suits your needs and preferences. Remember
to exercise caution while editing files, and make sure to have appropriate permissions to modify the files.
3. File Searching:
Here is a list of Linux commands for file searching:
1. `grep <pattern> <file>`: Search for a specific pattern within a file. It displays the lines containing the pattern.
Example: `grep "error" myfile.log`
2. `grep -r <pattern> <directory>`: Recursively search for a pattern within a directory and its subdirectories.
Example: `grep -r "keyword" /path/to/directory`
3. `find <directory> -name "<filename>"`: Search for files by name within a directory and its subdirectories.
Example: `find /path/to/directory -name "file.txt"`
4. `locate <filename>`: Quickly find files by name using a pre-built database. It provides fast search results but requires regular
updating of the database using the `updatedb` command.
Example: `locate file.txt`
5. `ack <pattern> <file(s)>`: A tool similar to `grep` specifically designed for code searching. It searches for a pattern within specified
files or directories.
Example: `ack "function_name" myfile.py`
6. `ag <pattern> <file(s)>`: Another code searching tool similar to `ack`. It is known for its speed and support for regular expressions.
Example: `ag "class_name" myfile.py`
These commands help you search for specific patterns or files within your Linux system. Choose the appropriate command based on
your requirements, whether it's searching for patterns in file contents or locating files by name.
.
4. File Comparison:
Here is a list of Linux commands for file comparison:
1. `diff <file1> <file2>`: Compare two files line by line and display the differences between them.
Example: `diff file1.txt file2.txt`
2. `vimdiff <file1> <file2>`: Compare two files in the Vim editor and highlight the differences.
Example: `vimdiff file1.txt file2.txt`
3. `cmp <file1> <file2>`: Compare two files byte by byte and report the first differing byte and its offset.
Example: `cmp file1.txt file2.txt`
4. `comm <file1> <file2>`: Compare two sorted files line by line and display lines that are unique to each file or common to both files.
Example: `comm file1.txt file2.txt`
5. `sdiff <file1> <file2>`: Compare two files side by side and highlight differences.
Example: `sdiff file1.txt file2.txt`
These commands provide different ways to compare files in Linux. Choose the appropriate command based on your specific needs
and the type of comparison you want to perform, whether it's comparing line by line, byte by byte, or side by side.
5. File Content Manipulation:
Here is a list of Linux commands for file content manipulation:
1. `sed 's/<pattern>/<replacement>/g' <file>`: Replace occurrences of a pattern with a replacement string in a file.
Example: `sed 's/foo/bar/g' myfile.txt` (Replaces all occurrences of "foo" with "bar" in myfile.txt)
2. `awk '<pattern> {<action>}' <file>`: Perform actions on specific patterns within a file using the AWK scripting language.
Example: `awk '/pattern/ {print $1}' myfile.txt` (Prints the first field of lines containing "pattern" in myfile.txt)
3. `cut -d'<delimiter>' -f <fields> <file>`: Extract specific fields from a file based on a delimiter.
Example: `cut -d',' -f 1,3 myfile.csv` (Extracts the first and third fields from myfile.csv using comma as the delimiter)
7. `paste -d'<delimiter>' <file1> <file2>`: Merge lines from multiple files side by side with a specified delimiter.
Example: `paste -d',' file1.txt file2.txt` (Merges lines from file1.txt and file2.txt with comma as the delimiter)
These commands allow you to manipulate the content of files in various ways, such as replacing text, extracting fields, sorting,
removing duplicates, and more. Choose the appropriate command based on your specific requirements and the desired content
manipulation.
.
These are some commonly used Linux commands for viewing and editing files. Each command has its own set of options and
functionalities to suit different needs. Refer to the respective command's documentation or use the `man` command for more
information on their usage and options.
.
C. File Permissions
Here is a list of Linux commands related to file permissions:
1. `ls -l`: List files and directories in long format, displaying detailed information including permissions.
Example: `ls -l`
3. `chown <user>:<group> <file>`: Change the owner and group of a file or directory.
Example: `chown user1:group1 myfile.txt` (Changes the owner to user1 and the group to group1 for myfile.txt)
5. `umask`: Set the default file permissions mask for newly created files and directories.
Example: `umask 0022` (Sets the default permissions mask to allow read and write access to the owner and read access to others)
D. System Information
Here is a list of Linux commands to obtain system information:
1. `uname`: Display system information such as the kernel name, network node hostname, kernel release, kernel version, machine
hardware name, and processor type.
Example: `uname -a`
4. `uptime`: Show the system's uptime, load average, and number of logged-in users.
Example: `uptime`
5. `free`: Display information about available and used system memory (RAM).
Example: `free -h`
8. `top`: Display real-time system resource usage including CPU, memory, and process information.
Example: `top`
10. `lscpu`: Display detailed information about the CPU architecture and characteristics.
Example: `lscpu`
11. `lsblk`: List block devices (disks and partitions) on the system.
Example: `lsblk`
14. `netstat`: Display network connections, routing tables, and network statistics.
Example: `netstat -tuln`
These commands provide various system information, including kernel details, hostname, user information, uptime, memory usage,
disk space, process information, CPU details, network configuration, hardware information, and more. Use the appropriate command
to obtain the specific information you need.
E. Package Management:
Here is a list of Linux commands for package management:
1. `apt`: Advanced Package Tool (APT) is a package management system used by Debian-based distributions, such as Ubuntu.
- `apt update`: Update the package lists to get the latest version information of packages.
- `apt upgrade`: Upgrade installed packages to their latest versions.
- `apt install <package>`: Install a package.
- `apt remove <package>`: Remove a package.
- `apt search <keyword>`: Search for packages based on a keyword.
- `apt show <package>`: Display detailed information about a package.
2. `yum`: Yellowdog Updater Modified (YUM) is a package management system used by Red Hat-based distributions, such as CentOS
and Fedora.
3. `dnf`: Dandified YUM (DNF) is the next-generation package manager used by Fedora and CentOS 8+.
These are some of the commonly used package management commands on Linux. The exact commands may vary depending on the
specific Linux distribution you are using.
F. Networking
Here is a list of commonly used Linux networking commands:
5. `netstat`: Print network connections, routing tables, interface statistics, and more.
Example: `netstat -a`
10. `wget`: Retrieve files from the web using HTTP, HTTPS, or FTP.
Example: `wget https://example.com/file.txt`
11. `curl`: A versatile command-line tool for making HTTP, HTTPS, and FTP requests.
Example: `curl https://example.com`
12. `ssh`: Secure Shell client for secure remote login and command execution.
Example: `ssh user@hostname`
14. `iptables`: A powerful firewall management tool for configuring IP packet filtering and NAT rules.
Example: `iptables -L`
16. `nmap`: Network exploration and security auditing tool for scanning hosts and services.
Example: `nmap -sP 192.168.0.0/24`
19. `hostapd`: Host Access Point daemon for creating a wireless access point.
Example: `hostapd /etc/hostapd/hostapd.conf`
These commands cover a wide range of networking tasks, such as configuring interfaces, checking network connectivity, performing
DNS lookups, retrieving files, managing firewalls, monitoring network traffic, and more. Use the appropriate command based on your
specific networking requirements.
G. Process Management:
Here is a list of commonly used Linux commands for process management:
17. `lsof`: List open files and the processes using them.
Example: `lsof /path/to/file`
These commands help you manage processes on a Linux system, including listing processes, terminating processes, changing
process priorities, managing jobs, monitoring system resources, controlling system services, and more. Use the appropriate
command based on your specific process management needs.
5. `rar` / `unrar`: Create or extract RAR archives (if the `rar` package is installed).
- Create a rar archive: `rar a archive.rar /path/to/file1 /path/to/file2`
- Extract a rar archive: `unrar x archive.rar`
9. `tar.gz` / `tar.bz2` / `tar.xz`: Create or extract tar archives with compression directly.
- Create a compressed tar archive (gzip): `tar -cvzf archive.tar.gz /path/to/directory`
- Extract a compressed tar archive (gzip): `tar -xvzf archive.tar.gz`
- Create a compressed tar archive (bzip2): `tar -cvjf archive.tar.bz2 /path/to/directory`
- Extract a compressed tar archive (bzip2): `tar -xvjf archive.tar.bz2`
- Create a compressed tar archive (xz): `tar -cvJf archive.tar.xz /path/to/directory`
- Extract a compressed tar archive (xz): `tar -xvJf archive.tar.xz`
These commands allow you to create and extract compressed and archived files
I. System Administration
Here are some commonly used Linux commands for system administration tasks:
1. User Management:
Here are some commonly used Linux commands for user management:
14. Revoke sudo access from a user (requires sudo privileges): `visudo`
This opens the sudoers file. Remove the line granting sudo access for the user.
Example: `john ALL=(ALL:ALL) ALL`
These commands allow you to create, modify, delete, and manage user accounts in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.
2. Group Management:
Here are some commonly used Linux commands for group management:
These commands allow you to create, modify, and manage groups in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.
3. Password Management:
Here are some commonly used Linux commands for password management:
8. Enable password authentication for a user (if previously disabled): `usermod -p '' <username>`
Example: `usermod -p '' john`
10. Set password complexity requirements: This may vary depending on the Linux distribution and
configuration. Please refer to the specific documentation for your distribution.
11. Reset a forgotten root password: This process can vary depending on the Linux distribution. It often
involves booting into recovery mode or using a live CD/USB. Please refer to the specific documentation for
your distribution.
Remember to execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`)
when required. Additionally, ensure that you follow best practices for password security, such as using
strong and unique passwords, regularly updating passwords, and implementing multi-factor
authentication where possible.
12. Show the groups a user belongs to (alternative to `groups` command): `getent group | grep
<username>`
Example: `getent group | grep john`
These commands allow you to obtain information about users and groups in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.
5. chown and chgrp recursive: Change ownership and group ownership recursively for a directory and its
contents.
- Syntax: `chown -R [OPTIONS] OWNER[:GROUP] DIRECTORY`
- Syntax: `chgrp -R [OPTIONS] GROUP DIRECTORY`
- Example: `chown -R john:users directory` (Recursively change the owner of `directory` and its contents
to user `john` and the group to `users`)
7. umask: Set the default file permissions for newly created files and directories.
- Syntax: `umask [OPTIONS] [MODE]`
- Example: `umask 0022` (Set the default file permissions to `644` and default directory permissions to
`755`)
These commands allow you to manage file ownership and permissions in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.
3. System Information:
Here are some commonly used Linux commands for obtaining system information in system
administration:
4. Process Management:
- List running processes: `ps aux`
- Kill a process: `kill <PID>`
- Monitor system processes: `top` or `htop`
- Start a background process: `command &`
Here are some commonly used Linux commands for process management in system administration:
5. Networking:
- Here are some commonly used Linux commands for networking in system administration:
1. ifconfig: Configure and display network interfaces.
- Syntax: `ifconfig [OPTIONS] [INTERFACE]`
- Example: `ifconfig eth0` (Display information for the interface "eth0")
9. ssh: Secure shell client for remote login and secure file transfer.
- Syntax: `ssh [OPTIONS] [USER@]HOST`
- Example: `ssh user@example.com` (Establish an SSH connection to the host "example.com" as the
user "user")
6. Package Management:
- Here are some commonly used Linux commands for package management in system administration:
1. apt-get: Command-line package handling utility for Debian-based systems (e.g., Ubuntu).
- Syntax: `apt-get [OPTIONS] COMMAND`
- Example: `apt-get install packageName` (Install a package)
2. apt: Command-line interface for package management in Debian-based systems (e.g., Ubuntu). It is the
successor to `apt-get` and provides more advanced features.
- Syntax: `apt [OPTIONS] COMMAND`
- Example: `apt install packageName` (Install a package)
7. System Services:
- Here are some commonly used Linux commands for managing system services in system
administration:
These commands are commonly used for managing system services, such as starting, stopping,
enabling/disabling services, and checking their status. Note that the exact commands and service
management tools may vary depending on the Linux distribution you are using and whether it utilizes
System V init or systemd.
8. Log Files:
- Here are some commonly used Linux commands for working with log files in system administration:
1. tail: Display the last lines of a file (typically used for log files).
- Syntax: `tail [OPTIONS] FILE`
- Example: `tail -f /var/log/syslog` (Display the last lines of the syslog file and continuously update as
new lines are added)
2. head: Display the first lines of a file (can be useful for inspecting log files).
- Syntax: `head [OPTIONS] FILE`
- Example: `head -n 20 /var/log/auth.log` (Display the first 20 lines of the auth.log file)
3. cat: Concatenate and display the contents of a file (can be used to view log files).
- Syntax: `cat [OPTIONS] FILE`
- Example: `cat /var/log/messages` (Display the contents of the messages log file)
7. dmesg: Print and control the kernel ring buffer messages (includes boot and hardware-related logs).
- Syntax: `dmesg [OPTIONS]`
- Example: `dmesg | grep "error"` (Display kernel messages containing the word "error")
8. find: Search for log files based on various criteria (e.g., filename, modified date).
- Syntax: `find [PATH] [CRITERIA]`
- Example: `find /var/log -name "*.log"` (Find all log files with the .log extension in the /var/log directory)
These commands provide various ways to view, search, and analyze log files in Linux. They can help you
troubleshoot issues, monitor system activities, and extract relevant information from log files. Remember
to use appropriate options and filters to refine your search or display specific log entries as needed.