Introduction to Unix Commands
Unix commands are the backbone of interacting with Unix-like operating systems such as Linux, macOS, and BSD. These commands perform a variety of tasks ranging from file management to system monitoring and process control. Understanding the basic syntax and options for these commands is crucial for effective system management.
Basic File and Directory Management Commands
Listing Files and Directories
- ls: Lists directory contents.
Common options:
ls -l
: Detailed list including permissions, owner, size, and modification date.ls -a
: Includes hidden files (those starting with a dot).ls -lh
: Human-readable sizes with detailed info.
Creating Files and Directories
- touch: Creates an empty file or updates the timestamp of an existing file.
- mkdir: Creates a new directory.
Examples:
touch newfile.txt
mkdir new_directory
Removing Files and Directories
- rm: Deletes files or directories.
Options:
rm -r
: Recursively removes directories and their contents.rm -f
: Forces deletion without prompts.
Examples:
rm file.txt
rm -rf directory_name
Copying and Moving Files
- cp: Copies files or directories.
Options:
cp -r
: Recursively copies directories.
Examples:
cp source.txt destination.txt
cp -r dir1 dir2
Renaming Files
- mv: Moves or renames files and directories.
Example:
mv oldname.txt newname.txt
Viewing and Manipulating File Content
Displaying File Contents
- cat: Concatenates and displays file contents.
- less: Views file contents one screen at a time with navigation.
- more: Similar to less but with limited navigation features.
Examples:
cat file.txt
less largefile.log
Searching Within Files
- grep: Searches for patterns within files.
Common usage:
grep "search_term" filename
grep -r "pattern" directory/ Recursive search
Counting Lines, Words, and Characters
- wc: Counts lines, words, and characters.
Examples:
wc -l file.txt Lines
wc -w file.txt Words
wc -c file.txt Characters
File Permissions and Ownership
Changing Permissions
- chmod: Modifies file permissions.
Examples:
chmod 755 script.sh rwxr-xr-x
chmod u+x file.sh Adds execute permission for the owner
Changing Ownership
- chown: Changes file owner and group.
Examples:
chown user:group file.txt
Process Management Commands
Viewing Processes
- ps: Displays current processes.
Common options:
ps aux
: Shows all processes with detailed info.ps -ef
: Another format for process listing.
Monitoring Real-Time Processes
- top: Displays real-time process activity.
- htop: An enhanced, interactive version of top (if installed).
Terminating Processes
- kill: Sends signals to processes to terminate or restart them.
Examples:
kill PID Graceful termination
kill -9 PID Forceful kill
Disk Usage and Filesystem Commands
Checking Disk Space
- df: Reports filesystem disk space usage.
Options:
df -h Human-readable format
Checking Directory Size
- du: Shows disk usage of files/directories.
Examples:
du -sh directory_name Summarized, human-readable size
Mounting and Unmounting Filesystems
- mount: Mounts a filesystem.
- umount: Unmounts a filesystem.
Examples:
mount /dev/sdX1 /mnt/point
umount /mnt/point
User Management Commands
Adding and Removing Users
- adduser: Adds a new user (varies by distribution).
- deluser: Removes a user.
Switching Users
- su: Switches to another user.
- sudo: Executes commands with superuser privileges.
Examples:
su - username
sudo apt update
Managing User Groups
- groupadd: Creates a new group.
- groupdel: Deletes a group.
- usermod: Modifies user account details, including group memberships.
Networking Commands
Checking Network Configuration
- ifconfig: Displays network interfaces (deprecated in favor of ip command).
- ip addr: Shows IP addresses and interface info.
Testing Network Connectivity
- ping: Checks if a host is reachable.
Example:
ping google.com
Tracing Network Routes
- traceroute: Shows the path packets take to reach a host.
Managing Open Ports and Services
- netstat: Displays network connections and listening ports.
- ss: Modern replacement for netstat.
Examples:
ss -tuln List all listening ports
Compression and Archiving Commands
Creating Archives
- tar: Archives files and directories.
Examples:
tar -cvf archive.tar directory/
tar -czvf archive.tar.gz directory/ Compressed with gzip
Frequently Asked Questions
What are some essential Unix commands for navigating the file system?
Common navigation commands include 'ls' to list directory contents, 'cd' to change directories, 'pwd' to print the current directory, and 'tree' to visualize directory structures.
How do I view the contents of a file in Unix?
Use commands like 'cat' to display the entire file, 'less' or 'more' for paginated viewing, and 'head' or 'tail' to see the beginning or end of a file.
What is the purpose of the 'grep' command in Unix?
'grep' searches for specific patterns or strings within files, making it useful for filtering and finding information quickly.
How can I manage processes using Unix commands?
Use 'ps' to list running processes, 'kill' to terminate processes, 'top' or 'htop' for real-time process monitoring, and 'killall' to stop processes by name.
What are some commands for file manipulation in Unix?
Commands like 'cp' to copy files, 'mv' to move or rename, 'rm' to delete, and 'mkdir' to create new directories are essential for file management.
How do I search for files or directories in Unix?
Use 'find' to locate files and directories based on various criteria, and 'locate' for quick searches in a pre-built database. 'which' helps find the path of executable commands.