Page 1 of 9
1. Saving Results to File
To save command output to a file:
-
Command
> file
(Overwrites command output) -
Command
>> file
(Appends output)
Example:
echo $NAME > file
2. Basic Commands
mv- Move files/directoriescp- Copy files/directoriesrm- Remove files/directoriesmkdir- Create new directoriesecho- Display textls- List filesls -f- Reverse file orderls -t- Sort by time
touch- Create empty filespwd- Print working directorycat- Concatenate file contentcd- Change directorycd ..- Move to parent directorycd -- Move to previous directory
3. File Permissions
Viewing Permissions
Use ls -l to view file permissions:
Permissions Owner Group Size Date FileName
-rw-rw-r-- samarth samarth 4096 Aug 10 SPOJ
Permission Types
Permissions are divided into:
- User (u)
- Group (g)
- Other (o) (people outside the group)
Permissions:
r→ Readw→ Writex→ Execute
Example
chmod u+w,g+w FileName
This gives the user and group write permissions.
Page 2 of 9
Removing Permissions
chmod u-rw,g-rwx FileName
Shorthand for Permissions
Permissions can also be set using numeric shorthand:
chmod 754 FileName
Where:
- 7 → User: read, write, execute
- 5 → Group: read, execute
- 4 → Other: read
Numeric Representation:
4 → Read
2 → Write
1 → Execute
0 → No Permissions
4. Checksum
Commands to generate checksums:
sha1sum FileName
sha256sum FileName
md5sum FileName
5. Compress & Extract .tar and .gz Files
Compress
gzip FileNametar -cvf zipName.tar FileName
Extract
gunzip FileName.gztar -xvf zipName.tar
Page 3 of 9
6. Users
Viewing Users
Command:
useradd samarth
This creates a user named samarth.
Command:
sudo passwd samarth
Sets a password for samarth.
7. Groups
Managing Groups
Commands:
sudo groupadd <groupName> # Add new group
sudo usermod -a -G <groupName> <user> # Append user to group
sudo usermod -G <groupName> <user> # Replace primary group
8. Viewing Files
Commands to view file contents:
cat FileName # View entire file
more FileName # View file content page-by-page
less FileName # Scroll through file
head FileName # View the first 10 lines
tail FileName # View the last 10 lines
9. Wildcards
Wildcards are used for pattern matching in file names:
*→ Matches zero or more characters
Example:a*→ Matchesabc,a123, etc.?→ Matches a single character
Example:a?→ Matchesab,ac, etc.[abc]→ Matches any of the specified characters
Example:[a-c]→ Matchesa,b,c.
Page 4 of 9
Advanced Wildcards
[!ae]→ Does not start withaore[a-g]*→ Starts withatog[3-6]*→ Starts with3to6
10. diff & grep
Compare Files
diff file1 file2
Search Patterns in Files
grep <pattern> FileName
Options:
-i→ Ignore case-c→ Count occurrences-n→ Include line numbers-v→ Inverse match (lines that don't match)
File Type Information
Commands:
file FileName # Display file type
strings FileName # Display printable strings in file
11. Pipe Symbol
Use | to pass output of one command as input to another:
cat FileName | grep <pattern>
ls -au | grep <node>
12. Copying Files Over the Network
Commands:
- Using
scp:
scp <source> <destination>:/path
- Using
sftp:
sftp <host> # Example: samarth@remote.com
Commands within sftp:
lcd # Change to local directory
put FileName # Upload file
get FileName # Download file
Page 5 of 9
Processes
- ps -e: Display all processes
- ps -eH: Display process tree
- top: Interactive process viewer
- ps -p PID: Display process by PID
Background & Foreground
- command &: Start command in the background
- ctrl-c: Kill foreground process
- ctrl-z: Suspend the foreground process
- bg [%num]: Background a suspended process
- fg [%num]: Foreground a background process
- kill: Kill a process by PID or process number
- kill -9 PID: Forcefully kill a process
Cron Service
- cron: A time-based job scheduling service
- crontab: Perform CRUD operations on job schedules
- Used to automate and schedule jobs
User Switching
- whoami: Displays the current user
- su [username]: Change user or become superuser
- sudo: Superuser do (temporary elevated permissions)
- sudo -u [user] command: Execute command as specified user
- sudo su - [username]: Switch to another user
Page 6 of 9
Shell History
- Shell history is stored in
~/.bash_history - !N: Repeat command line number N
- !string: Repeat most recent command starting with "string"
- history: View command history with numbers
- ctrl-R: Reverse search
- ctrl-C: Cancel search
Tab Autocompletion
- Double tab: Get list of possible commands
- Tab: Autocompletion
Package Management
- DEB: Debian packages
- APT: Advanced Packaging Tool
Common Commands
- apt-cache search [string]: Search for a package
- apt-get install [package]: Install a package
- apt-get remove [package]: Remove a package (leaves configuration)
- apt-get purge [package]: Delete package (including configuration)
- apt-cache show [package]: Display details about a package
Examples
- apt-cache search wireshark
- apt-cache show wireshark
- apt-get install wireshark
- which wireshark:
/usr/bin/wireshark - dpkg -i chrome.deb: Install
chrome.debpackage - dpkg -l: List all packages
- dpkg -s /usr/bin/wireshark: Display package details
Page 7 of 9
Disk Management
- fdisk: Manage partitions (allows data separation)
Common Commands
- sudo fdisk -l: List all partitions
- sudo fdisk /dev/sda: Manage partitions on device
/dev/sda- m: Help
- Partition management options
File Systems
- ext: Linux file system
Page 8 of 9
User & Group Management
Account Details
Each account (user) has:
- Username
- UID
- Default Group
- Comments
- Shell
- Home Directory Location
User Information
- All user details are stored in
/etc/passwdfile- Format:
username:password:UID:GID:comments:home_dir:shell - Encrypted passwords are stored in
/etc/shadow
- Format:
User Management Commands
- sudo useradd [username] -c "comment": Add a new user
- passwd [username]: Set or change user password
- There are other users apart from root, such as:
- sshd, mongo, syslog: Used for system or application accounts
User Deletion
- sudo userdel [username]: Delete a user
- sudo userdel -r [username]: Delete user and associated files
User Update
- sudo usermod [username] [options]: Update user properties
Page 9 of 9
Managing Groups in Linux
All group details are stored in /etc/group.
The format of a group entry is:
groupname:password:GID:account1,account2,...,accountN
- password: Encrypted (shadow)
- GID: Group ID
- account1, account2, ...: List of accounts in the group.
Commands for Group Management
-
List all groups:
groups username -
Add a new group:
groupadd group-name -
Delete a group:
groupdel group-name -
Modify a group:
groupmod group-name
Note: Replace group-name and username with the appropriate group name and username values.