Basic Linux Commands:
sudo:
Description: Allows a permitted user to execute a command as the
superuser or another user.
Syntax: sudo [options] command
Example: sudo apt-get update
man:
Description: Displays the manual pages of Unix-like operating systems.
Syntax: man [options] command
Example: man ls
echo:
Description: Displays a line of text/string that is passed as an
argument.
Syntax: echo [options] [string]
Example: echo "Hello, World!"
printf:
Description: Allows for formatted printing of text/string.
Syntax: printf format [arguments]
Example: printf "My name is %s\n" "John"
script:
Description: Records a terminal session, including all input and output.
Syntax: script [options] [file]
Example: script session.log
passwd:
Description: Allows users to change their passwords or other users'
passwords if granted permission.
Syntax: passwd [options] [username]
Example: passwd
uname:
Description: Displays system information.
Syntax: uname [options]
Example: uname -a
who:
Description: Shows who is logged on and information about their terminal
session.
Syntax: who [options]
Example: who
date:
Description: Displays or sets the system date and time.
Syntax: date [options] [+FORMAT]
Example: date
stty:
Description: Changes and prints terminal line settings.
Syntax: stty [options]
Example: stty -a
pwd:
Description: Prints the current working directory.
Syntax: pwd [options]
Example: pwd
cd:
Description: Changes the current directory.
Syntax: cd [directory]
Example: cd /home/user
mkdir:
Description: Creates a new directory.
Syntax: mkdir [options] directory
Example: mkdir new_directory
rmdir:
Description: Removes a directory.
Syntax: rmdir [options] directory
Example: rmdir empty_directory
ls:
Description: Lists directory contents.
Syntax: ls [options] [file(s)]
Example: ls -l
cp:
escription: Copies files and directories.
Syntax: cp [options] source destination
Example: cp file1.txt file2.txt
mv:
Description: Moves or renames files and directories.
Syntax: mv [options] source destination
Example: mv file1.txt new_directory
rm:
Description: Removes files or directories.
Syntax: rm [options] file(s)
Example: rm file.txt
cat:
Description: Concatenates and displays the content of files.
Syntax: cat [options] [file(s)]
Example: cat file.txt
more:
Description: Displays the content of a file one screen at a time.
Syntax: more [options] [file(s)]
Example: more file.txt
wc:
Description: Counts lines, words, and characters in a file.
Syntax: wc [options] [file(s)]
Example: wc -l file.txt
lp:
Description: Sends files to a printer for printing.
Syntax: lp [options] [file(s)]
Example: lp file.txt
od:
Description: Displays the content of files in octal or other formats.
Syntax: od [options] [file(s)]
Example: od -c file.txt
tar:
Description: Creates or extracts files from an archive file.
Syntax: tar [options] [file(s)]
Example: tar -cvf archive.tar directory
gzip:
Description: Compresses or decompresses files.
Syntax: gzip [options] [file(s)]
Example: gzip file.txt
Networking Commands:
unlink:
Description: Deletes a file.
Syntax: unlink [options] file
Example: unlink file.txt
du:
Description: Displays disk usage of files and directories.
Syntax: du [options] [directory]
Example: du -sh /var/log
df:
Description: Displays disk space usage.
Syntax: df [options] [file(s)]
Example: df -h
mount:
Description: Mounts a filesystem.
Syntax: mount [options] device directory
Example: mount /dev/sdb1 /mnt
umount:
Description: Unmounts a mounted filesystem.
Syntax: umount [options] directory
Example: umount /mnt
find:
Description: Searches for files and directories in a directory hierarchy.
Syntax: find [path...] [expression]
Example: find /home/user -name "*.txt"
unmask:
Description: Sets or displays the file creation mask.
Syntax: umask [options]
Example: umask
ulimit:
Description: Sets or displays user limits.
Syntax: ulimit [options]
Example: ulimit -a
ps:
Description: Displays information about processes.
Syntax: ps [options]
Example: ps aux
w:
Description: Shows who is logged on and what they are doing.
Syntax: w [options]
Example: w
finger:
Description: Displays information about users.
Syntax: finger [options] [username]
Example: finger user
arp:
Description: Displays and manipulates the ARP cache.
Syntax: arp [options]
Example: arp -a
ftp:
Description: Transfers files to and from a remote network site.
Syntax: ftp [options] [hostname]
Example: ftp ftp.example.com
telnet:
Description: Connects to remote hosts using the Telnet protocol.
Syntax: telnet [options] [hostname]
Example: telnet example.com
rlogin:
Description: Connects to a remote host using the rlogin protocol.
Syntax: rlogin [options] [hostname]
Example: rlogin remotehost
Text Processing Commands:
tail:
Description: Displays the last part of a file.
Syntax: tail [options] [file(s)]
Example: tail -n 10 file.txt
head:
Description: Displays the first part of a file.
Syntax: head [options] [file(s)]
Example: head -n 5 file.txt
sort:
Description: Sorts lines of text files.
Syntax: sort [options] [file(s)]
Example: sort file.txt
nl:
Description: Numbers lines in a file.
Syntax: nl [options] [file(s)]
Example: nl file.txt
uniq:
Description: Removes duplicate lines from a sorted file.
Syntax: uniq [options] [file]
Example: uniq file.txt
grep:
Description: Searches for patterns in files.
Syntax: grep [options] pattern [file(s)]
Example: grep "pattern" file.txt
egrep:
Description: Searches for extended regular expressions in files.
Syntax: egrep [options] pattern [file(s)]
Example: egrep "pattern1|pattern2" file.txt
fgrep:
Description: Searches for fixed strings in files.
Syntax: fgrep [options] pattern [file(s)]
Example: fgrep "pattern" file.txt
cut:
Description: Extracts sections from each line of files.
Syntax: cut [options] [file(s)]
Example: cut -d"," -f1 file.csv
paste:
Description: Merges lines of files.
Syntax: paste [options] [file(s)]
Example: paste file1.txt file2.txt
join:
Description: Joins lines of two files on a common field.
Syntax: join [options] file1 file2
Example: join file1.txt file2.txt
tee:
Description: Reads from standard input and writes to standard output and
files.
Syntax: tee [options] [file(s)]
Example: command | tee output.txt
pg:
Description: Displays files one page at a time.
Syntax: pg [options] [file(s)]
Example: pg file.txt
comm:
Description: Compares two sorted files line by line.
Syntax: comm [options] file1 file2
Example: comm file1.txt file2.txt
cmp:
Description: Compares two files byte by byte.
Syntax: cmp [options] file1 file2
Example: cmp file1.txt file2.txt
diff:
Description: Compares files line by line.
Syntax: diff [options] file1 file2
Example: diff file1.txt file2.txt
tr:
Description: Translates characters.
Syntax: tr [options] set1 [set2]
Example: tr 'a-z' 'A-Z' < file.txt
cpio:
Description: Copies files to and from archives.
Syntax: cpio [options]
Example: find . | cpio -o > archive.cpio
System Calls for File Management:
create:
Description: Creates a new file with the specified name and attributes.
Syntax: Not directly invoked by the user; part of system calls.
open:
Description: Opens an existing file or creates a new file and associates
a file descriptor with it.
Syntax: Not directly invoked by the user; part of system calls.
close:
Description: Closes a file descriptor, releasing associated system
resources.
Syntax: Not directly invoked by the user; part of system calls.
read:
Description: Reads data from a file into a buffer.
Syntax: Not directly invoked by the user; part of system calls.
write:
Description: Writes data from a buffer to a file.
Syntax: Not directly invoked by the user; part of system calls.
lseek:
Description: Moves the read/write pointer to a specified position in a
file.
Syntax: Not directly invoked by the user; part of system calls.
link:
Description: Creates a new link to an existing file.
Syntax: Not directly invoked by the user; part of system calls.
symlink:
Description: Creates a symbolic link to a file.
Syntax: Not directly invoked by the user; part of system calls.
unlink:
Description: Deletes a name and possibly the file it refers to.
Syntax: Not directly invoked by the user; part of system calls.
stat:
Description: Returns information about a file.
Syntax: Not directly invoked by the user; part of system calls.
fstat:
Description: Returns information about an open file.
Syntax: Not directly invoked by the user; part of system calls.
lstat:
Description: Returns information about a symbolic link.
Syntax: Not directly invoked by the user; part of system calls.
chmod:
Description: Changes file permissions.
Syntax: Not directly invoked by the user; part of system calls.
chown:
Description: Changes file ownership.
Syntax: Not directly invoked by the user; part of system calls.
Directory API:
opendir:
Description: Opens a directory for reading.
Syntax: Not directly invoked by the user; part of directory API.
readdir:
Description: Reads a directory entry.
Syntax: Not directly invoked by the user; part of directory API.
closedir:
Description: Closes a directory.
Syntax: Not directly invoked by the user; part of directory API.
mkdir:
Description: Creates a new directory.
Syntax: Not directly invoked by the user; part of directory API.
rmdir:
Description: Removes an empty directory.
Syntax: Not directly invoked by the user; part of directory API.
umask:
Description: Sets the default file permissions mask.
Syntax: Not directly invoked by the user; part of directory API.
File Links - Hard and Soft Links:
Hard Links:
Description: Hard links are directory entries that directly point to the
physical location of a file on disk. They share the same inode as the
original file and have the same permissions and metadata.
Syntax: ln [options] target link_name
Example: ln /path/to/original_file /path/to/hard_link
Soft Links (Symbolic Links):
Description: Symbolic links are special files that point to another file
or directory by name. They contain the path of the target file instead of
pointing directly to its inode.
Syntax: ln -s target link_name
Example: ln -s /path/to/target_file /path/to/symbolic_link
Environment and Path Setting:
Environment Variables:
Description: Environment variables are dynamic values that can affect the
behavior of processes and programs in a Unix-like operating system.
Setting an Environment Variable: export VARIABLE=value
Example: export PATH=/usr/local/bin:$PATH
Path Setting:
Description: The PATH environment variable specifies the directories in
which the shell searches for executable files.
Syntax: export PATH=/path/to/directory:$PATH
Example: export PATH=/usr/local/bin:$PATH
/etc/passwd and /etc/shadow Files:
/etc/passwd:
Description: The /etc/passwd file stores essential information about user
accounts on a Unix-like system, such as username, user ID, group ID, home
directory, and default shell.
Example Entry: john:x:1000:1000:John Doe:/home/john:/bin/bash
/etc/shadow:
Description: The /etc/shadow file stores encrypted password information
for user accounts, as well as other security-related data such as
password aging policies.
Example Entry:
john:$6$0LP3Vh.b$lsC3lB01Xc0qzGGV5YFxR6RGm68g/9bZg3fP6kSh/3eqvlVrlBCB7hqd
PzJtnZfPTcZN8w0tIek1F1lT90JgN.:18810:0:99999:7:::
Add, Modify, and Delete Users:
Adding a User:
Description: Use the useradd command to add a new user to the system.
Syntax: sudo useradd [options] username
Example: sudo useradd john
Modifying User Attributes:
Description: Use the usermod command to modify user account attributes
such as username, home directory, shell, etc.
Syntax: sudo usermod [options] username
Example: sudo usermod -d /new/home/directory john
Deleting a User:
Description: Use the userdel command to delete a user account from the
system.
Syntax: sudo userdel [options] username
Example: sudo userdel john
These commands and files are crucial for managing user accounts and
permissions on a Unix-like operating system, providing administrators
with the necessary tools to control access and maintain system security.
Compiling & Linking C, C++ Programs:
Compiling C Programs:
Description: Compilation is the process of translating source code
written in C into machine-readable object code. The gcc command is
commonly used for compiling C programs. Options like -Wall enable
warnings for potential issues in the code.
Example: In the command gcc -Wall program.c -o program, program.c is the
source file, and -o program specifies the output executable file name.
Compiling C++ Programs:
Description: Similar to compiling C programs, compiling C++ programs
involves translating source code written in C++ into object code. The g++
command is used for compiling C++ programs.
Example: In the command g++ -std=c++11 -Wall program.cpp -o program, -
std=c++11 specifies the C++ standard, and program.cpp is the C++ source
file.
Linking Programs:
Description: Linking is the process of combining multiple object files
and libraries into a single executable file. It resolves references
between different parts of the program and produces an executable file.
Example: In the command gcc -o program program.o utility.o -lm, program.o
and utility.o are object files, and -lm links the math library. The -o
option specifies the output executable file name.
Make Utility:
Description:
Description: Make is a build automation tool that simplifies the process
of compiling and linking programs from source code. It reads instructions
from a file called a makefile to determine how to build the program.
Example: A makefile contains rules for building targets (e.g., executable
files) from dependencies (e.g., source files). Each rule specifies the
commands needed to build the target.
Debugging C Programs Using GDB:
Description:
Description: GDB (GNU Debugger) is a powerful tool for debugging C and
C++ programs. It allows developers to inspect the program's state, set
breakpoints, step through code, and examine variables.
Example: After starting GDB with gdb program, developers can set
breakpoints using the break command, run the program with run, and
examine variables with the print command.
Installing, Updating, and Deleting Software Packages:
apt-get (APT Package Manager):
Description: APT (Advanced Package Tool) is a command-line package
management tool used by Debian-based Linux distributions. It simplifies
the process of installing, updating, and removing software packages.
Example: sudo apt-get install package_name installs a package, sudo apt-
get update updates package lists, and sudo apt-get remove package_name
removes a package.
rpm (RPM Package Manager):
Description: RPM is a package management system used by Red Hat-based
Linux distributions. It provides similar functionality to APT but uses
different commands and package formats.
Example: rpm -i package.rpm installs a package, rpm -U package.rpm
updates a package, and rpm -e package_name removes a package.
dpkg (Debian Package Manager):
Description: DPKG is a low-level package management tool used by Debian-
based Linux distributions. It works directly with individual package
files and provides basic package management functionality.
Example: sudo dpkg -i package.deb installs a package, sudo dpkg -r
package_name removes a package.
Source Code Management - RCS and CVS:
RCS (Revision Control System):
Description: RCS is a legacy version control system that tracks changes
to individual files. It is suitable for small projects or individual
files but lacks features found in modern version control systems.
Usage: Developers use commands like ci (check-in) to commit changes and
co (check-out) to retrieve the latest version of a file.
CVS (Concurrent Versions System):
Description: CVS is an older version control system that supports
concurrent development by multiple users. It allows developers to work on
the same project simultaneously and manage changes to files.
Usage: CVS provides commands like checkout to retrieve a working copy of
a project, update to synchronize with the repository, and commit to
submit changes.
Awk Utility:
Description: Awk is a versatile text processing language used for pattern
scanning and processing files. It operates on records (lines) and fields
(words) and allows developers to perform complex text manipulation tasks.
Example: In the command awk '/pattern/ {print $1}' file.txt, Awk searches
for lines matching the pattern and prints the first field of each
matching line.
Basic Commands:
Print Fields: Awk provides variables like $1, $2, etc., to access fields
in a record.
Pattern Matching: Awk supports pattern matching using regular
expressions.
Variables: Awk provides built-in variables like NR (Number of Records),
NF (Number of Fields), and $0 (Whole Line) for processing files.