0% found this document useful (0 votes)
18 views8 pages

Essential Linux Command Guide

This document provides a comprehensive guide to basic Linux commands, covering system information, file and directory management, user and group administration, file permissions, process management, disk management, and networking commands. Each section includes specific commands along with their usage and examples. It serves as a quick reference for users to perform various tasks in a Linux environment.

Uploaded by

amr77eldeeb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Essential Linux Command Guide

This document provides a comprehensive guide to basic Linux commands, covering system information, file and directory management, user and group administration, file permissions, process management, disk management, and networking commands. Each section includes specific commands along with their usage and examples. It serves as a quick reference for users to perform various tasks in a Linux environment.

Uploaded by

amr77eldeeb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic Linux commands

System Information

Show computer architecture:

arch

Show kernel version

uname -r

Show system date

date

Show calendar table for 2014

cal 2014

Files and Directories

Go to '/var/www/'

cd /var/www

Go to the upper level directory

cd ..

Go to home directory

cd ~

Go to home directory of “user”

cd ~user

Show the contents of the current directory

ls

Show the contents of the current directory adding characters to the names characterizing the type

ls -F
Show detailed representation of files and directories in the current directory

ls -l

Show hidden files and directories in the current directory

ls -a

Create a directory called ‘test’

mkdir test

Create two directories simultaneously

mkdir test1 test2

Create a directory tree

mkdir -p /var/www/test/dir

Delete the file named ‘file’

rm -f file

Delete a directory named ‘dir’

rmdir dir

Delete a directory named ‘dir’ and all of its contents recursively

rm -rf dir

Rename or move a file or directory

mv dir1 new_dir

Copy file1 in file2

cp file1 file2

Create a symbolic link to a file or directory

ln -s file1 file2

Searching Files
Find files and directories named file1. Start searching from the root (/)

find / -name file1

Find files and directories owned by user1. Start searching from the root (/)

find / -user user1

Find all files and directories whose names end in ‘.log’. Start searching from ‘/var/www’

find /var/www -name "*.log"

Find all files containing '.png' in the name. it is recommended to run 'updatedb' command before
this.

locate "*.png"

Find all files with '.log' extension in the current directory, including subdirectories and delete
them

find . -name '*.log' -type f -delete

Disk Space

Information about partitioned sections displaying total, free and used space

df -h

Show the size used by directory ‘dir1’

du -sh dir1

Users and Groups

Create user1, assign /home/user1 as its home directory, assign /bin/bash as a shell, include it in
‘admin’ group and add a comment Nome Cognome

useradd -c "Nome Cognome" -g admin -d /home/user1 -s /bin/bash


user1

Create user1

useradd user1

Delete user1 and its home directory


userdel -r user1

Create a new group named group_name

groupadd group_name

Delete group group_name

groupdel group_name

Rename group old_group_name in new_group_name

groupmod -n new_group_name old_group_name

Change password

passwd

Change password of user1 (only root)

passwd user1

Setting/Changing File Rights

Add rights 777 (Read Write Execute) to directory1 – full rights to everybody.

chmod 777 directory1

Add rights to directory1, including all files and subfolders in it, rights 777 (Read Write Execute)
- full rights to everybody.

chmod –R 777 directory1

Assign user1 as the owner of file1

chown user1 file1

Recursively assign user1 as the owner of directory1

chown -R user1 directory1

Assign user 'apache' from the group 'apache' to folder 'dir', including all subfolders and files:

chown apache:apache -R /var/www/dir

Find all files in the current directory, including subfolders and assign rights 664
find . -type f -printf "\"%p\" " | xargs chmod 664

Find all folders in the current directory, including subfolders and assign rights 775

find . -type d -printf "\"%p\" " | xargs chmod 775

File Archiving and Compression

Unarchive file 'file1.bz2'

bunzip2 file1.bz2

Unarchive file '[Link]'

gunzip [Link]

Archive file 'file1' to [Link]

gzip file1

Archive file file1 to file1.bz2

bzip2 file1

Create an archive and zip it with gzip

tar -cvfz [Link] dir1

Unarchive and extract

tar -xvfz [Link]

Create zip-archive

zip [Link] file1

Unarchive and extract zip-archive

unzip [Link]

Upgrading Packages

Upload and install package

yum install package_name


Update all packages installed in the system

yum update

Update package

yum update package_name

Delete package

yum remove package_name

Show the list of all packages installed in the system

yum list

Find a package in the repository

yum search package_name

Clean rpm-cache, deleting uploaded packages

yum clean packages

Clean rpm-cache, deleting uploaded packages and headers

yum clean all

Help Commands

It is used to find concise descriptions of system commands by searching the Whatis


whatis
database for entire words.
which Displays full path to shell commands
whereis Search and discover command's binary, source, and man pages
Look for help and man pages containing specific phrases and instructions by searching
apropos
a database of short descriptions.
man Lists Manual pages for the respective command

File Operations

cat Concatenate files or print a file on the monitor.


touch Make a new file and modify the timestamps.
cp Copy files or directories.
mv Moves files or directories.
rm Removes directories or files in a recursive manner
ln It is used for creating a symbolic or hard link for a file.
less Viewing a single file on a single page, along with the ability to go backwards.
head Printing the first ten lines of a file.
tail Printing the last ten lines of a file.
wc Counts the total number of words or characters there are in a file.
stat Shows the status of a file or a file system.
cut Remove parts of input lines.
paste Combine multiple lines of text in a single file.

Process Management

ps Provide an update on existing procedures.


pstree Displays a tree of processes.
top Demonstrate real-time procedures.
kill PID is used to terminate a process.
killall Stop a process permanently via name
pkill Looking up for processes or signals based on the same and other attributes.
pgrep Look for information on the process.
ctrl+z Stop the task instead of keeping it on halt permanently.
& The addition of this after a command puts the process in the background.
jobs Displays all the jobs running in process

Partitions and Disk Management

df Report on the amount of disc space used by the file system.


mount Mount a file system or see what's mounted.
unmount Unmounts a file system
fuser Identifies processes via files or sockets.
isof Displays a list of the system's open files

Accounts and Security Administration

groupadd Used for creating new groups


groupdel Used for creating groups
groupmod Manipulating the description of a particular group
useradd Creating new users
userdel Deleting new users
usermod Modifying a user’s account
passwd Updating a user’s password
vipw Editing the password or shadow password of a group
vigr Editing groups or shadow groups
chage Modifying password policies

Networking

ifconfig Set up the network interface.


route Shows and manipulates IP routing table
Replaces ifconfig, arp, and route by displaying and manipulating routing, devices,
ip
policies, and tunnels.
ifup It brings up the network interfaces
ifdown It brings the network interfaces down
ping Sends ICMP ECHO_REQUEST to network hosts

Directory Operations

clear Removes all the contents from your screen


pwd Displays running directory on the screen
cd Modifies directories
ls Lists down all the content of a directory
mkdir Creates a new directory
rmdir Deletes null directories

Common questions

Powered by AI

`useradd` is used to create new user accounts, whereas `userdel` removes existing accounts and optionally their home directories. Precautions include ensuring no critical processes are being run by the user before deleting, as this could impact system stability. Moreover, it is important to backup user files and settings if they may be needed later .

To archive a directory using gzip, one would use the `tar` command with options `-cvfz`, e.g., `tar -cvfz archive.tar.gz directory_name`. This creates a compressed archive in the `.tar.gz` format. Gzip provides a good balance between compression efficiency and speed, making it suitable for large data sets where space saving is important but computational resources are limited. Compared to methods like bzip2, gzip is usually faster, though it may result in slightly larger files .

The `find` command combined with `xargs` and `chmod` is powerful for changing file permissions recursively. An example command is `find . -type f -printf "%p" | xargs chmod 664`, which assigns 664 permissions to all files. While effective, this method can inadvertently alter permissions on critical files if the path isn't specified precisely, leading to security risks, such as granting write permissions to unintended users on sensitive files .

The `df -h` command displays disk space usage in a human-readable format, showing total, used, and available space on mounted filesystems. This information aids in efficient disk management by identifying disks that are nearing capacity, enabling administrators to reallocate resources, delete unnecessary files, or plan for additional storage .

The `ln -s file1 file2` command creates a symbolic link, `file2`, pointing to `file1`. Symbolic links offer benefits such as saving disk space and improving file accessibility across directories without duplication. However, drawbacks include broken links if the source file (`file1`) is moved or deleted, and potential confusion for users who do not realize they're working with a link instead of the original file .

The `tail` command displays the last ten lines of a file, suitable for monitoring log files that append new entries at the end, facilitating real-time monitoring. The `head` command displays the first ten lines, ideal for reviewing the beginning of logs or files where setups or headers need initial assessment. Each is useful in different contexts depending on whether the data of interest is at the start or end of the file .

`locate` uses a pre-built database to quickly find files by name, offering much faster search performance over large file systems. It requires the database to be regularly updated with `updatedb` to reflect file changes. `find`, however, searches in real time and can filter results by more parameters like modification time or type, making it ideal for more complex searches. `locate` is preferable for speed when up-to-date, while `find` excels in precision and detail .

Using `chmod -R 777 directory` grants all users read, write, and execute permissions on the directory and its contents, creating significant security vulnerabilities as any user can modify, delete, or execute the files. This setting might be justified in a collaborative environment where rapid iteration is occurring and security is controlled through other means, or for troubleshooting permissions issues, though it should be temporary since it poses a risk of unauthorized access or damage .

To change the group ownership of a directory and all its contents, you would use the `chown` command with the `-R` option for recursion and specify the new group. The command syntax would be `chown :new_group -R directory_name`. When changing user ownership, you specify the username in place of the group name, like `chown user:new_group -R directory`, where `user` is the new owner. This approach differs as it allows distinct modification of either user or group ownership or both .

`yum update` updates all installed packages on the system to their latest available versions, which can introduce new features and security patches but may also cause compatibility issues if certain software interdependencies are not met. The command `yum install package_name`, on the other hand, selectively installs a specific package and any of its dependencies, minimizing the risk of broader system instability but without the benefit of upgrading existing packages to newer, possibly more secure versions .

You might also like