Some useful command lines for Linux systems

By admin, February 20, 2018

Once in a while, as programmers or system administrators, we face the need of issuing commands we might not remember by heart. So I came out with the following list (to be constantly updated) related to this topic.

I have been using mostly the CentOS and Ubuntu distros, so most of them may be applied in most Linux flavours.

Finding files and content

Find a file, by filename recursively:
find . -name “composer*”
Find a file, by filename, recursively, case insensitive
find . -print | grep -i composer.phar
To search inside symbolic links:
find -L . -name “composer*”
For find only FILES, mention -type f
find -L . -type f -name “composer*”
For find with REGEX
find -L . -type f -iname “composer.
Find file containing a text (pattern) -r = recursive, -n number of line -w stand for all word -l could be lower case

My prefered

grep -rnw ‘/path/to/somewhere/’ -e ‘pattern’

grep -rnw . –color –exclude-dir={custom,vendor} -e ‘$_GET’

Showing only folders when needed

grep -r [regex to find] [path to search] | awk -F: ‘{print $1}’ | uniq

Replacing recursively a text for another recursively

find /path/to/files -type f -exec sed -i ‘s/oldstring/new string/g’ {} \;

Check directory and files size in Linux

View directories
du -h dirunix/

View directories and files
du -ah dirunix/

View a summary
du -sh dirunix/

Empty big huge great files content in Linux
#du -sh access.log
1.1G
#> access.log
or
#true > access.log
or
#cat /dev/null > access.log
or
#cp /dev/null access.log
or
#echo -n “” > access.log # -n stands for null
or
#truncate -s 0 access.log

#du -sh access.log
0

ZIP File operations

Include just cetain types of files or directories
zip -r foo website_folder -i ‘*.php’ ‘*.html’ ‘*.js’ ‘*.css’

Exclude files or dir: -x e.g.
Directory: zip -r archivename.zip directorytozip -x “*.git*”
File: zip -r archive.zip directory -x “*.jpg”

Notepad++, perl, etc

Superfind – replace for rebuild a current line

Find  ^.*$
Replace (for preparing dirs to be copied
cp package201812/$0 $0
Or
$0\n$0
or what you want
Linux Disk Usage
du -hsx * | sort -rh | head -50
https://www.centos.org/forums/viewtopic.php?t=61945
Alternative:
yum install ncdu
Where is a installed package
That is simple, but might be tricky anyway.
$whereis jenkins
$whereis apache2
Deleting files older than X days in a given path
find /path/to/ -type f -mtime +7 -name ‘*.gz’ -delete
Where mtime is time in days you want go back
Deleting folders older than X days in a given path
find /dirtoerase -mtime +1 -type d | xargs rm -f -r
Where mtime is time in days you want go back
* You can add the parameters -maxdepth 1 -mindepth 1
Resizing Virtual Disks in Windows / CENTOS 7
Case 1 – Virtual Box
.\VBoxManage clonehd “C:\Users\smdev-02\VirtualBox VMs\vagrant_56\box-disk1.vmdk” “C:\Users\smdev-02\VirtualBox VMs\vagrant_56\cloned.vdi” –format vdi
.\VBoxManage modifyhd “C:\Users\smdev-02\VirtualBox VMs\vagrant_56\cloned.vdi” –resize 25600
.\VBoxManage clonehd “C:\Users\smdev-02\VirtualBox VMs\vagrant_56\cloned.vdi” “C:\Users\smdev-02\VirtualBox VMs\vagrant_56\resized.vmdk” –format vmdkfdisk -lcheck the last sdaN number. Create the next letter (4)
Case 2 – KVM
Open your host machine shell
check the existing instances
virsh list
pick the img name of the instance you want to increase the size
qemu-img resize dev-01.img +100G
Further info about Case 2:
https://computingforgeeks.com/virsh-commands-cheatsheet/
Now inside your virtualized server:

chech the name of the disks you have:

fdisk -l

Disk /dev/vda: 406.9 GB, 406948155392 bytes, 794820616 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0004d19d

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 209715207 103808004 8e Linux LVM

Run the following sequence of commands:

E.g. fdisk /dev/vda  (command n),(command p), (partition number – next free number), (enter for the cilinders) , (command t), (number of the instance 4), type 8e (Linux LVM), print part table (command p) write (command w) reboot

partprobe
pvcreate /dev/sda4
vgextend (volumeName) /dev/sda4

To check the volume name

vgs

e.g.

VG #PV #LV #SN Attr VSize VFree
vg_main 1 5 0 wz–n- <99.00g 1.00g

e.g. vgextend centos /dev/sda4
pvscan
lvsdisplay
lvextend /dev/centos/root /dev/sda4
resize2fs /dev/centos/root (not working)
xfs_growfs /dev/centos/root

References:
https://www.rootusers.com/how-to-increase-the-size-of-a-linux-lvm-by-expanding-the-virtual-machine-disk/

Verify the available ports in Linux systems
$netstat -lntu
$ss -lntu