Linux Commands:
Linux commands provide immense power in managing files and system resources. For developers, DevOps engineers, and system administrators, knowing the right commands can save time and avoid errors. This blog covers some of the most essential commands for managing files, directories making everyday tasks simpler and more efficient.
1. cat command
The cat command, short for "concatenate," is a fundamental Linux utility used to display the contents of files, combine files, and create new files. It's especially useful for quickly viewing the contents of text files in the terminal.
Example: To display the content of a file named courses.txt, you would use:
cat Courses.txt
2. history Command
The history command displays the command history for the current terminal session. This command is valuable for recalling previously executed commands, allowing users to save time by reusing them.
Example: To view the commands history executed in the terminal, you can use:history
Example 2: To view the last 10 commands executed in the terminal, you can use:
history 10
3. rm Command
The rm (remove) command is used to delete files and directories in Linux. It’s a powerful command, so caution is advised when using it, as deleted files cannot be easily recovered.
Example: To remove a directory use : rm -r directory name
The -r option is necessary to remove directories and their contents recursively.
4. Creating a File and Adding Content
To create a new file and add content to it, you can use the echo command along with output redirection.
Example: To create a file , add content, and display it, you can use:
echo "This is new file." > newfile.txt
cat newfile.txt
or create file newfile.txt and enter vim newfile.txt editor manually
then manually you can add the list in vim editor and save quit from the editor by giving esc:wq!
Now display the file by giving cat FrontendCourses.txt
5. head Command
The head command is used to display the beginning lines of a file. By default, it shows the first 10 lines, but you can customize this with options.
Example: To view the first 5 lines of a file named data.txt, use:head -n 4 data.txt
- head Command (Reverse Order)
The head command is used to display the beginning lines of a file. To see the first three lines in reverse order, you can combine head with tac (which reverses the lines).
Example: To view the first three lines of a file named courses.txt in reverse order, use:
head -n 3 Courses.txt | tac
6. tail Command
The tail command is the opposite of head, displaying the last lines of a file. It’s useful for monitoring log files or recent data entries.
Example: To view the last 3 lines of a file named Courses.txt, you can use:
tail -n 3 Courses.txt
7. Counting Lines, Words, and Characters
The wc (word count) command in Linux is a powerful utility used to count the number of lines, words, and characters in a file or input stream. It is often used for analyzing text files, scripts, or output from other commands.
wc -l: This option counts and displays the number of lines in the specified file.
wc -w: This option counts and displays the number of words in the specified file.
wc -c: This option counts and displays the number of characters in the specified file.
apt-get Command
apt-get is a command-line tool for handling packages in Debian-based Linux distributions (like Ubuntu). It allows users to install, update, upgrade, and remove software packages.
apt-get update: Updates the package list from the repositories. This is the first step before installing or upgrading packages.
Example:sudo apt-get update
apt-get upgrade: Upgrades all the installed packages to their latest versions without removing any packages.
Example:sudo apt-get upgrade
apt-get install: Installs a new package. You can specify the package name to install.
Example:sudo apt-get install package-name
apt-get remove: Removes an installed package but retains its configuration files.
Example:sudo apt-get remove package-name
apt-get purge: Removes an installed package along with its configuration files.
Example:sudo apt-get purge package-name
apt-get autoremove: Automatically removes packages that were installed as dependencies but are no longer needed.
Example:sudo apt-get autoremove
Conclusion:
Mastering these essential Linux commands can significantly streamline your workflow, whether you're managing files or organizing directories. By practicing these commands regularly, you'll improve your efficiency and become more comfortable navigating the command line. Linux offers an incredible range of tools to optimize your tasks—start incorporating these into your daily routine to harness the full power of the system.
Subscribe to my newsletter
Read articles from sravani punreddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by