Linux Commands
1. Basic Navigation Commands
pwd(Print Working Directory)Purpose: Displays the full path of the current directory you are in.
Example:
$PWD(returns something like/home/user/documents)
ls(List)Purpose: Lists the contents of a directory.
Common Options:
-l: Long listing format (shows permissions, owner, group, size, date, name).-a: All files (includes hidden files, which start with a dot.)-h: Human-readable sizes (e.g., )Example:
ls -lah
cd(Change Directory)Purpose: Changes the current working directory.
Common Usage:
cd /path/to/directory: Navigates to an absolute path.cd directory_name: Navigates to a subdirectory relative to the current directory.cd ..: Moves up one directory level.cd: Navigates to the user's home directory.cd -: Navigates to the previous working directory.Example:
cd ..
2. File Manipulation Commands
touchPurpose: Creates an empty file or updates the timestamp of an existing file.
Example:
touch newfile.txt
cp(Copy)Purpose: Copies files or directories.
Common Options:
-ror-R: Recursive (required for copying directories).-i: Interactive (prompts before overwriting).Example:
cp file.txt /path/to/destination/Example (directory):
cp -r directory/ /path/to/destination/
mv(Move)Purpose: Moves or renames files or directories.
Usage: Source and destination arguments similar to
cp.Example:
mv oldname.txt newname.txt(rename)Example:
mv file.txt /path/to/destination/(move)
rm(Remove)Purpose: Deletes files or directories.
Common Options:
-ror-R: Recursive (required for deleting non-empty directories).-f: Force (deletes without prompting).-i: Interactive (prompts before every deletion).Example:
rm oldfile.txtExample (directory):
rm -r olddirectory/
3. Directory Manipulation Commands
mkdir(Make Directory)Purpose: Creates a new directory.
Common Options:
-p: Parents (creates parent directories as needed).Example:
mkdir new_folderExample:
mkdir -p project/src/main
rmdir(Remove Directory)Purpose: Deletes an empty directory.
Note:
rm -ris typically used for non-empty directories.Example:
rmdir empty_folder
4. Viewing File Content
cat(Concatenate)Purpose: Displays the content of a file to standard output.
Example:
cat document.txt
lessPurpose: Allows viewing file content one screen at a time (scrollable).
Usage: Press
qto quit, arrow keys to scroll.Example:
less large_log_file.txt
headPurpose: Displays the beginning lines of a file (default: lines).
Common Options:
-n COUNT: Displays the firstCOUNTlines.Example:
head -n 5 file.txt
tailPurpose: Displays the ending lines of a file (default: lines).
Common Options:
-n COUNT: Displays the lastCOUNTlines.-f: Follows (monitors) the file for new content (useful for logs).Example:
tail -n 5 file.txtExample:
tail -f /var/log/syslog
5. User and Permissions
chmod(Change Mode)Purpose: Changes file permissions.
Permissions: Read (), Write (), Execute ()
Categories: User (), Group (), Others (), All ()
Symbolic Mode:
chmod u+x filename(add execute for user)Octal/Numeric Mode: Sum of permissions for each category ( etc.)
Example:
chmod 755 script.sh(user: rwx, group: r-x, others: r-x)Example:
chmod go-w file.txt(remove write for group and others)
chown(Change Owner)Purpose: Changes the owner of a file or directory.
Example:
chown user filenameExample (with group):
chown user:group filename
sudo(Substitute User Do)Purpose: Allows a permitted user to execute a command as the superuser or another user.
Example:
sudo apt update(requires password for elevated privileges)
whoamiPurpose: Displays the current user's username.
Example:
whoami
man(Manual)Purpose: Displays the manual page for a command.
Usage: Press
qto quit.Example:
man ls