Welcome, Linux newbies! If you’ve recently been on your journey into Linux, you’ve probably realized that the command line is your gateway to unlocking the full potential of this tiny powerhouse. Fear not! I’m here to guide you through the basics, one command at a time. As a system administrator who’s spent several hours working with Linux systems, I’ve grown to love the precision and control the command line offers, though I must admit, it was a bit daunting at first.
Why learn Linux commands?
Before we dive into the commands, let’s talk about why these commands matter. The Linux command line is a text-based interface used to control the operating system. Unlike the graphical user interface (GUI) that most people are used to, the command line provides a more direct and powerful means of interacting with the computer. For Raspberry Pi users, mastering these commands means you can troubleshoot issues, manage files, configure settings, and run software more efficiently.
20 basic Linux commands you should know
1. pwd
– Print Working Directory
The pwd
command is your starting point in the command-line journey. It tells you your current directory location in the file system. This is incredibly helpful when you’ve navigated through several directories and need a quick reminder of where you are. Think of it as asking for directions on a roadmap.
$ pwd /home/pi
2. ls
– List Directory Contents
The ls
command lists all files and directories in your current directory. It’s the most frequent command you’ll use to browse your filesystem and see what’s inside a folder. It’s quite versatile, offering various options to view detailed information about the files, such as file permissions, size, and modification date.
$ ls Desktop Documents Downloads Music Pictures Public Templates Videos
3. cd
– Change Directory
The cd
command changes your current directory. It’s how you navigate through the folders in your filesystem. Using cd
followed by a directory name moves you into that directory. Going back is simple too; just type cd ..
to go up one level, or cd
to return to your home directory.
$ cd Documents $ pwd /home/pi/Documents
4. mkdir
– Make Directory
When you need to create a new folder, mkdir
is your go-to command. It’s straightforward and essential for organizing files into different directories. Just type mkdir
followed by the name you wish to give the new directory, and it will be created in your current location.
$ mkdir NewFolder $ ls Desktop Documents Downloads Music NewFolder Pictures Public Templates Videos
5. rmdir
– Remove Directory
To delete an empty directory, rmdir
is the command to use. It’s a safe way to remove directories, as it only works on empty ones, ensuring you don’t accidentally delete files. If you need to delete a directory with files in it, you’ll need a more powerful command like rm -r
.
$ rmdir NewFolder $ ls Desktop Documents Downloads Music Pictures Public Templates Videos
6. touch
– Create Empty Files
touch
is a versatile command used primarily to create an empty file without opening an editor. It can also update the timestamps on existing files. It’s especially useful for scripting and when you need to quickly create or update files without any content modifications.
$ touch example.txt $ ls Desktop Documents Downloads example.txt Music Pictures Public Templates Videos
7. rm
– Remove Files
The rm
command is used to delete files and directories. It’s powerful and should be used with caution, as once a file is removed, it cannot be easily recovered. rm -r
is used for recursive deletion of directories and their contents, making it a potent tool for cleaning up files.
$ rm example.txt $ ls Desktop Documents Downloads Music Pictures Public Templates Videos
8. cp
– Copy Files or Directories
The cp
command copies files or directories from one location to another. It’s essential for duplicating files, creating backups, or moving data around. You can use various options to control its behavior, such as whether to copy directories recursively or to preserve file attributes.
$ cp source.txt destination.txt $ ls Desktop Documents Downloads Music Pictures Public source.txt destination.txt Templates Videos
9. mv
– Move or Rename Files or Directories
mv
is a dual-purpose command, used for moving files and directories from one location to another or renaming them. When you move a file, it’s removed from the original location and appears in the new one. When you rename a file, its name changes but it stays in the same location.
$ mv oldname.txt newname.txt $ ls Desktop Documents Downloads Music newname.txt Pictures Public Templates Videos
10. cat
– Concatenate and Display File Content
cat
is one of the simplest yet most frequently used commands. Primarily, it reads and displays the content of files. Additionally, it can concatenate (combine) multiple files into one. This command is often used for quickly viewing small files and for combining text files.
$ cat example.txt Hello, Raspberry Pi!
11. nano
– Text Editor
nano
is a user-friendly, lightweight text editor for the command line. It’s less complex than other editors like vim
or emacs
, making it ideal for beginners or for quick edits. It shows a helpful list of shortcuts at the bottom, making it easy to use.
$ nano example.txt
12. sudo
– SuperUser DO
sudo
allows a permitted user to execute a command with the privileges of another user, typically the superuser (root). It’s crucial for tasks that require administrative or elevated permissions, like installing software or changing system configurations.
$ sudo apt-get update
13. apt-get
– Package Management
Part of the Advanced Packaging Tool (APT), apt-get
is used for managing software packages on Debian-based systems. You can install, update, and remove software packages easily using this command. It handles dependencies and ensures that your software is kept up-to-date.
$ sudo apt-get install python3
14. chmod
– Change File Permissions
The chmod
command changes the file permissions. In Linux, file permissions control who can read, write, or execute a file. This command is essential for managing file security and access control, and it uses a numeric or symbolic method to set permissions.
$ chmod 755 script.sh $ ls -l script.sh -rwxr-xr-x 1 pi pi 0 Jan 30 10:00 script.sh
15. chown
– Change File Owner and Group
chown
changes the ownership of a file or directory. It’s used to assign a new owner and group to a file. This is important in scenarios where files need to be shared or restricted among different users or groups on the system.
$ chown pi:pi example.txt $ ls -l example.txt -rw-r--r-- 1 pi pi 0 Jan 30 10:05 example.txt
16. find
– Search for Files
find
searches for files and directories based on different criteria like name, size, and modification date. It’s extremely powerful for locating files and directories in a large filesystem, offering a high level of precision in your searches.
$ find / -name example.txt 2>/dev/null /home/pi/Documents/example.txt
17. grep
– Search Inside Files
grep
searches for patterns in text. It’s a versatile tool for filtering and finding specific lines in files or command outputs. Whether you’re looking for a text string in a file or filtering the output of another command, grep
is your go-to.
$ grep "Hello" *.txt example.txt:Hello, Raspberry Pi!
18. df
– Disk Space Usage
df
provides a quick overview of your filesystem’s disk space usage. It shows how much space is occupied and available on each mounted filesystem. This command is useful for monitoring disk space, especially on systems with limited storage.
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 59G 4.5G 53G 8% /
19. du
– Disk Usage of Files and Directories
The du
command is your go-to for checking the size of files and directories. It helps in identifying how much space a particular file or directory occupies, which is vital for managing disk space effectively.
$ du -sh /home/pi 1.2G /home/pi
20. top
– Task Manager
top
gives a dynamic, real-time view of the running processes in your system. It shows CPU and memory usage, among other statistics, and is incredibly useful for monitoring system performance and troubleshooting issues.
top - 10:10:00 up 1 day, 2:43, 2 users, load average: 0.00, 0.01, 0.05 Tasks: 117 total, 1 running, 116 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.7 us, 0.2 sy, 0.0 ni, 98.9 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 926.1 total, 267.8 free, 401.6 used, 256.7 buff/cache
21. ps
– Process Status
ps
displays information about active processes. It’s a snapshot of what’s currently running on your system, offering insights into resource usage and process states, which is invaluable for system management and debugging.
$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND pi 1024 0.0 0.5 10836 5480 pts/0 Ss 10:10 0:00 bash
22. kill
– Kill Processes
The kill
command sends signals to a specified process, usually to terminate the process. It requires the process ID (PID) and is a direct way to control processes that are unresponsive or consuming too many resources.
$ kill -9 PID
23. man
– Manual Pages
man
displays the manual pages of commands. It’s the most comprehensive source of information for understanding command usage, options, and syntax. Whenever you’re in doubt about how a command works, man
is the first place to turn to.
$ man ls
24. echo
– Display a Line of Text
echo
is simple but useful for displaying text or variables. It’s often used in scripting to show messages.
$ echo "Hello, World!" Hello, World!
25. wget
– Network Downloader
Last but not least, wget
is fantastic for downloading files from the internet directly to your Raspberry Pi.
$ wget http://example.com/file.zip --2024-01-30 10:15:42-- http://example.com/file.zip Resolving example.com (example.com)... 93.184.216.34 Connecting to example.com (example.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/zip] Saving to: ‘file.zip’
FAQ: Basic Linux Commands on Raspberry Pi
What is the best way to learn Linux commands?
The best way to learn Linux commands is by practice. Start with basic commands like ls
, cd
, pwd
, and mkdir
, and use them in your daily tasks. Reading documentation and using the man
command to explore command options and usage can also be incredibly helpful.
How can I undo a command in Linux?
Linux does not have a universal “undo” command. The ability to undo an action depends on the command executed. For example, if you delete a file with rm
, you cannot undo this action (unless you have a backup). Always double-check commands, especially those that modify or delete data.
Can I use these commands on any Linux distribution?
Yes, most of the commands listed are standard and can be used across various Linux distributions, not just on Raspberry Pi OS. However, package management commands like apt-get
are specific to Debian-based distributions. Other distributions might use different package managers, like yum
or dnf
for Fedora and pacman
for Arch Linux.
What should I do if a command doesn’t work?
If a command doesn’t work, first check for typos. Linux commands are case-sensitive and must be typed exactly. If the command is correct, use the man
command to ensure you’re using the correct syntax and options. For example, man ls
will show you the manual for the ls
command.
How can I see all the commands I’ve typed?
You can view your command history by typing the history
command. This will list the commands you’ve recently used in your terminal session. You can also search through your history by pressing Ctrl + R
and then typing part of the command.
Can I run Windows commands on Raspberry Pi?
Raspberry Pi OS is a Linux-based system, so it does not natively support Windows commands. However, some commands have equivalents or alternatives in Linux. For example, the Windows dir
command is similar to the Linux ls
command.
How do I know which version of a program I have installed?
You can check the version of most programs by running the program with the --version
or -v
option. For example, to check the version of Python installed, you could type python3 --version
.
What is the difference between sudo
and su
?
sudo
allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers
file. su
, on the other hand, is used to switch to another user account. Without any arguments, su
switches to the root account. sudo
is considered safer than su
because it grants superuser privileges only temporarily and only for specific commands.
How do I get help for a specific command?
To get help for a specific command, you can usually use the --help
option with the command, which provides a brief overview of how to use it. For more detailed information, the man
command is the best option, e.g., man grep
.
What is the best way to manage files and directories in Linux?
The best way to manage files and directories in Linux is by using command-line tools like ls
for listing contents, cd
for changing directories, mkdir
and rmdir
for creating and removing directories, cp
and mv
for copying and moving files, and rm
for deleting files. These commands give you precise control over file and directory management.
Wrapping up
Congratulations! You’ve just taken a significant step towards mastering the Linux command line on your Raspberry Pi. While some of these commands might seem basic, they form the foundation of much more complex operations you’ll encounter as you delve deeper into the world of Linux.
The Linux command line is a powerful tool, but with great power comes great responsibility. Use these commands wisely, experiment with caution, and always make sure to back up your important data before attempting risky operations.