Understanding Basic Linux Commands
In Linux, commands are the building blocks for interacting with the system through the terminal. The terminal (or command line) is a powerful tool that allows users to perform tasks efficiently, from simple file management to complex system configurations.
Here’s an overview of some of the most essential basic Linux commands you'll use regularly:
1. Navigating the Filesystem
These commands are used to move around the system's file structure.
pwd
(Print Working Directory):
Shows the current directory you are in.$ pwd /home/user
cd
(Change Directory):
Changes the current directory.cd ~
– Takes you to your home directory.cd /
– Takes you to the root directory.cd ..
– Moves up one directory level.cd -
– Takes you to the previous directory.
$ cd /home/user/Documents
ls
(List):
Lists the contents of the current directory.ls -l
– Displays a detailed list (including permissions, owner, file size).ls -a
– Lists all files, including hidden files (files starting with a dot).
$ ls $ ls -l $ ls -a
2. Working with Files and Directories
These commands help you manage files and directories.
mkdir
(Make Directory):
Creates a new directory.$ mkdir new_folder
rmdir
(Remove Directory):
Removes an empty directory.$ rmdir empty_folder
rm
(Remove):
Deletes files or directories.rm file.txt
– Deletes a file.rm -r folder
– Deletes a directory and its contents recursively.rm -f
– Forces deletion without prompting for confirmation.
$ rm file.txt $ rm -r folder_name
cp
(Copy):
Copies files or directories.cp source_file destination_file
– Copies a file.cp -r source_directory destination_directory
– Copies a directory recursively.
$ cp file.txt copy_file.txt $ cp -r folder_name backup_folder
mv
(Move/Rename):
Moves or renames files or directories.mv old_file new_file
– Renames a file.mv file.txt /path/to/directory/
– Moves a file to another directory.
$ mv old_file.txt new_file.txt
touch
:
Creates an empty file or updates the timestamp of an existing file.$ touch new_file.txt
3. Viewing File Contents
These commands allow you to view the contents of files.
cat
(Concatenate):
Displays the contents of a file.$ cat file.txt
more
:
Displays the contents of a file one page at a time.$ more file.txt
less
:
Similar tomore
but allows both forward and backward navigation.$ less file.txt
head
:
Displays the first few lines of a file (default is 10 lines).$ head file.txt
tail
:
Displays the last few lines of a file (default is 10 lines).$ tail file.txt
4. Managing Permissions
These commands are used to manage file and directory permissions.
chmod
(Change Mode):
Changes the permissions of a file or directory.chmod 755 file
– Sets permissions to read, write, execute for the owner, and read-execute for others.chmod +x file
– Adds executable permissions.
$ chmod 755 file.txt $ chmod +x script.sh
chown
(Change Owner):
Changes the ownership of a file or directory.chown user:group file
– Changes the owner and group of a file.
$ chown user:group file.txt
chgrp
(Change Group):
Changes the group ownership of a file or directory.$ chgrp group_name file.txt
5. Searching for Files
These commands help you search for files and text within files.
find
:
Finds files and directories by criteria such as name, type, or size.$ find /home/user -name "*.txt"
grep
(Global Regular Expression Print):
Searches for a pattern within files.$ grep "search_term" file.txt
locate
:
Quickly finds files by name (using a prebuilt database).$ locate file.txt
6. System Information
These commands provide useful system-related information.
uname
:
Displays system information (kernel version, architecture, etc.).uname -a
– Displays detailed information.
$ uname -a
top
:
Displays real-time system resource usage (CPU, memory, processes).$ top
df
(Disk Free):
Displays disk space usage for all mounted file systems.$ df -h
du
(Disk Usage):
Displays the disk space used by files and directories.$ du -sh /path/to/directory
free
:
Displays memory usage (RAM).$ free -h
7. Process Management
These commands are used to view and manage running processes.
ps
(Process Status):
Displays information about running processes.ps aux
– Lists all running processes.
$ ps aux
kill
:
Terminates a process by its PID (process ID).$ kill 1234
killall
:
Terminates processes by name.$ killall process_name
htop
:
A more advanced and interactive version oftop
that shows resource usage and allows you to manage processes.$ htop
8. Networking Commands
These commands allow you to manage and troubleshoot network connections.
ping
:
Tests network connectivity to a host.$ ping google.com
ifconfig
(Interface Configuration):
Displays or configures network interfaces.$ ifconfig
ip
:
A more modern tool for managing network interfaces and addresses.$ ip addr show
netstat
(Network Statistics):
Displays network connections, routing tables, interface statistics, etc.$ netstat -tuln
9. Archiving and Compression
These commands allow you to compress and decompress files.
tar
:
Archives multiple files into a single file.tar -czf archive.tar.gz folder
– Compresses a folder into a.tar.gz
archive.tar -xzf archive.tar.gz
– Extracts a.tar.gz
archive.
$ tar -czf archive.tar.gz folder_name $ tar -xzf archive.tar.gz
zip
andunzip
:
Compresses files into.zip
format and extracts.zip
archives.$ zip archive.zip file.txt $ unzip archive.zip
Conclusion
These basic Linux commands form the foundation of your interactions with the Linux operating system. Mastery of these commands will greatly enhance your efficiency and ability to manage and navigate a Linux-based system. Once you become comfortable with these, you can dive into more advanced commands and tasks.