Common Linux Operating System Commands

08-01-2024 02:05:49

File Handling

List directory contents:
ls -l, to display file size in megabytes use ls -lh

Execute a file:
sh ./filename

Find the location of a specified file:
whereis filename

Create a new directory:
mkdir directoryname

View file content:
cat filename

Edit file:
vi filename
i to enter edit mode, allowing for modifications, :q! to force quit without saving, :wq to save and exit
/xxx to search for content "xxx", / to repeat the last search

File Operations

Copy files:
cp file1 file2 (copies existing file1 to a new file named file2)
cp file1 /archive (copies file1 to the /archive directory)
cp file1 mystuff/newfile (copies file1 to the mystuff directory in the current directory; if newfile is a subdirectory, it is copied into this subdirectory, otherwise, file1 is copied and named as newfile in the mystuff directory)

Move and rename:
mv file1 file2 (renames file1 to file2)
mv file1 /dir2 (moves file1 to the dir2 directory under the root directory)
mv file1 /dir2/file2 (if file2 is a directory, file1 is moved to the dir2/file2 directory. If file2 doesn't exist, file1 is moved and renamed to file2 in dir2)

Delete:
rm filename (deletes a file)
rm -R directoryname (deletes a directory)
rm -f filename (force delete without confirmation)
rm -R -f directoryname (force delete entire directory)

System Information

View partition:
df -h

View hard disk:
fdisk -l

View file count:
df -i

View total number of files in a directory:
find . -type f | wc -l

View directory capacity:
du -sh directoryname

View memory:
free (look at the total item)

View current processes, memory, and CPU usage:
top

View CPU information:
cat /proc/cpuinfo

View operating system:
cat /etc/release

Display or modify current time:
date -s
clock -w

System Management

Restart server:
reboot

Shutdown server:
shutdown -h now

Change server password:
passwd

Restart network:
service network restart

Stop firewall:
service iptables stop

Permanently disable firewall:
chkconfig iptables off

System repair: (Execute in single-user mode, cannot be done remotely, must be done via console or locally)
fsck

Network Services

Restart/start/stop Apache:
service httpd restart/start/stop

View PHP version:
php -v

View PHP modules:
php -m

Compression and Decompression

Unzip zip files:
unzip *.zip

Compress an entire directory: (Directory name DirName, excluding .gz files)
zip -r FileName.zip DirName -x *.gz

Unzip tar.gz files:
tar -zxvf *.tar.gz

Other Commands

Change permissions of an entire directory:
chmod 777 -R directoryname

Change owner of an entire directory:
chown -R user.user directoryname

Download files from the internet:
wget downloadpath

Install or update components:
yum install componentname
yum upgrade componentname

Transfer a file from one server to another:
scp -P portnumber (default 22) filename root@newserverIP:newserverpath