====== Linux Commands ====== The somewhat scary crevices of the command line. Credit for the documentation of many of the commands in this file goes to the [[http://www.git-tower.com/blog/command-line-cheat-sheet-detail/|incredible resource provided by Tower]]. =====Basic Commands ===== cp -r /home/user/* /var/www/html - copy all files in user's home directory to ''var/www/html'' folder mv wordpress.zip old.wordpress.zip - rename a file rm -rf wordpress.zip - Remove file rm -rf wordpress/* - Remove all files from folder ''wordpress/'' and all subfolders and files as well. yes | cp -rf /home/direcotry/to/folder /home/folder/to/copy/to - copy folder and say yes to overwrite ===== Debugging ===== tail -f - Running this command followed by a file path will output the the last few lines of a file in the command line window, and continue to display additions to the bottom of the file until typing ''ctrl c''. This is extremely handy for monitoring a WordPress ''debug.log'' file in the ''wp-content'' directory. To output the log, both ''WP_DEBUG'' and ''WP_DEBUG_LOG'' need to be true in your ''[[http://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Logging|wp-config.php]]''[[http://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Logging| file]]. ctrl-c - MAKE IT STOP! This will generally stop most commands in the middle of execution. ===== Commands ===== > - Direct the output of '''' into '''' replacing any current contents. >> - Append the output of '''' to ''''. | - Direct the output of '''' to ''''. For instance, if you want to copy the result of a command such as ''pwd'' to your clipboard using the handy ''pbcopy'' command from above you could do ''pwd | pbcopy'', which would put your current directory into your clipboard! clear - Clear the command line window. ===== Permissions ===== stat -c %a FILE_OR_FOLDER_PATH stat --format '%a' - see the chmod value of a directory or file Folder permissions are change in exactly the same way. For example: chmod 755 images will change the permissions to 755 of a folder called images that's in the current working directory. This, however, will only change the permissions of the folder itself. If you want to change the permissions recursively, meaning to change the permissions of the folder and everything in it, you can use the -R option. For example: chmod -R 755 images will change the permissions to 755 of the folder images and all the subfolders and files inside it. You can also change the permissions of several folders with one command by listing them (e.g. chmod 755 files images scripts). The chmod command can also be very useful in combination with other commands. For example, if you want to change the permissions of all files but you don't want the permissions of the folders to be changed, you can execute the following command: find . -type f -exec chmod 644 {} \; It will change the permissions of all files in the current working directory and those in all its subdirectories to 644. In a similar way you can change the permissions of all folders without modifying the permissions of files. The following command: find . -type d -exec chmod 755 {} \; will change the permissions of all directories in the current working directory (and the subdirectories of those directories) to 755. chown -R owner:group directory How to check if a particular domain is owned by any particular user: /scripts/whoowns mydomain.com owner OR grep mydomain.com /etc/userdomains =====Find/Replace===== find . -name %%'*%%.' -exec sed -i "" 's///g' {} \; - Finds the string '''' and replaces it with '''' in all '''' files within the directory. ===== Search ===== find . -type d -name "stuff" - **Directory ** - To find a directory specify the option -type d with find. find . -name myFile.txt - ** File** - This will return a list of the current folder's files that match ''myFile.txt'' and output it's location. find -name "" - Find all files named '''' inside '''' (use wildcards %%[*%%] to search for parts of filenames, e.g. "file.*"). grep "" - Output all occurrences of '''' inside '''' (add -i for case-insensitivity). grep -rl "" - Search for all files containing '''' inside ''''. grep -r "192.168.1.5" /etc/ - This will recursively search the files in the ''/etc/'' folder for the string ''192.168.1.5''. It can be useful for finding CSS classes in JS and CSS files as well. ===== Network ===== ping - Ping '''' and display status. whois - Output whois information for ''''. curl -O - Download '''' (via HTTP[S] or FTP). ssh @ - Establish an SSH connection to '''' with user ''''. scp @:/remote/path - Copy '''' to a remote ''''. sudo dhclient eno1 - Windows equivalent of dhcp release / renew sudo dhclient eno1 down sudo dhclient eno1 up ===== MySQL Commands ===== Sign in mysql -u username -p Show Databases: SHOW DATABASES; Choose database: USE DBNAME Show Tables: SHOW TABLES; Remove table DROP DATABASE dbname; Create database CREATE DATABASE dbname; Import mysql -p -u username database_name < file.sql Export mysqldump -p -u wpuser wp-db-1 > database-bkp.sql ... ===== ZIP/UNZIP Commands ===== zip -r filename.zip foldername/ tar -xvzf filename.tar.gz * f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file. * z: tells tar to decompress the archive using gzip * x: tar can collect files or extract them. x does the latter. * v: makes tar talk a lot. Verbose output shows you all the files being extracted. Not sure what this one does: tar cjf .tar.bz2 .. ===== List All Users / Create New User ===== list all users: cat -d: -f1 /etc/passwd add user: adduser newuser change users: su - newuser add user to sudoers file (as root): visudo add this line ''newuser ALL=(ALL:ALL) ALL'' /* ~~~~~~~~~~~~~~~ Special command for creating a new user and assigning them to a new usergroup and with that ownership ~~~~~~~~~~~~~~~~~*/ sudo adduser \ --system \ --shell /bin/bash \ --gecos 'Tomcat Java Servlet and JSP engine' \ --group \ --disabled-password \ --home /home/tomcat \ tomcat OUTPUT LOOKS LIKE THIS: ''Adding system user 'tomcat' (UID 108) ... Adding new group 'tomcat' (GID 113) ... Adding new user 'tomcat' (UID 108) with group 'tomcat' ... Creating home directory '/home/tomcat' ...'' ===== ZONE FILES / CPANEL / WHM ===== ls -ld /var/named/mydomain.com.db - How to check if the zone file for the domain exists /scripts/killdns mydomain.com - remove zone File ===== WORDPRESS fix all folder and file permissions!!! Run this in the root of you WordPress install!!! ===== find . -type f -name "wp-config-sample.php" -o -name "readme.html" -o -name "README.txt" -o -wholename "wp-admin/install.php" -exec rm -f {} \; && find . -type d -exec chmod 0755 {} \; && find . -type f -exec chmod 0644 {} \; && find . -name "wp-config.php" -exec chmod 600 {} \; ===== Mounting Disks / Disk Utility ===== sudo fdisk -l - Check disks for file system type mount -t ext3 /dev/sdb1 /home/jim/folder - mount a drive to a folder (Like USB drive) du -sh - find out how much storage the current folder consumes du -h --max-depth=1 . - show human readable storage of current folder /* ~~~~~~~~~~~~~~~ Special command - that will show you a bar graph (horizontal) in command line showing the directories that consume most of the space in a percentage ~~~~~~~~~~~~~~~~~*/ t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');du / –exclude /proc –exclude /sys –max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x OUTPUT LOOKS LIKE THIS: ''||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| / 74.9925% total |||||||||||||||||||||||||||||||||||| /home 42.0249% total ||||||||||||||||||||| /usr 23.4493% total ||||||||||||| /usr/local 15.2291% total ||||||||||| /usr/local/cpanel 12.4911% total |||||||| /home/southerntatler 9.18143% total |||||||| /home/southerntatler/public_html 9.11341% total |||||||| /home/southerntatler/public_html/wp-content 8.70109% total ||||||| /home/theloft 7.90133% total ||||||| /home/vqdev 7.8221% total ||||||| /home/vqdev/public_html 7.80835% total |||||| /home/southerntatler/public_html/wp-content/infinitewp 6.7047% total |||||| /home/southerntatler/public_html/wp-content/infinitewp/backups 6.70468% total |||||| /home/theloft/public_html 6.11472% total |||||| /home/theloft/public_html/wp-content 5.947% total ||||| /home/roswellghost 4.80236% total ||||| /home/roswellghost/public_html 4.71811% total |||| /var 4.47821% total'' ===== CPU / RAM / RESOURCE MANAGEMENT ===== echo ''; sar -q -f /var/log/sa/sa01 | grep -E '[1-9]+[0-9]*[0-9]*\...' | awk -v one=' 1 Minute Load Average' '{print $1, $2, $5, one}'; echo '' - (Prints just the 1 minute load average) ''Change the grep -E number to get a specific load average'' echo ''; sar -q -f /var/log/sa/sa01 | grep -E '[1-9]+[0-9]*[0-9]*\...' | awk -v five=' 5 Minute Load Avg' '{print $1, $2, $6, five}'; echo '' - (Prints just the 5 minute load average) echo ''; sar -q -f /var/log/sa/sa01 | grep -E '[1-9]+[0-9]*[0-9]*\...' | awk -v fifteen=' 15 Minute Load Avg' '{print $1, $2, $7, fifteen}'; echo '' - (Prints just the 15 minute load average) ===== Date and time ===== [[https://www.howtogeek.com/410442/how-to-display-the-date-and-time-in-the-linux-terminal-and-use-it-in-bash-scripts/]] ===== Restart Services ===== service named restart - restart apache /scripts/restartsrv_apache - restart apache on cPanel/WHM/CENTOS server: service iptables restart - restart IP tables to make DNS resolve faster ===== Hardware Information / Software Information ===== env | grep '^DESKTOP_SESSION' | cut -d'=' -f2- - will print 'ubuntu' uname -r - kernel version [kernel version, major, minor, bug-fix, distribution (generic)] uname -a - sysinfo [linux, hostname, kernel release, ubuntu compiled time, machine arch, processor arch, OS arch, GNU/Linux] cat /proc/version - similar to uname -a dmesg | grep Linux - similar to uname -a cat /etc/*-release lscpu - The lscpu command reports information about the cpu and processing units. It does not have any further options or functionality. sudo lshw -short - A general purpose utility, that reports detailed and brief information about multiple different hardware units such as cpu, memory, disk, usb controllers, network adapters etc. Lshw extracts the information from different /proc files. hwinfo --short - Hwinfo is another general purpose hardware probing utility that can report detailed and brief information about multiple different hardware components, and more than what lshw can report. lspci - The lspci command lists out all the pci buses and details about the devices connected to them. The vga adapter, graphics card, network adapter, usb ports, sata controllers, etc all fall under this category. lsscsi - Lists out the scsi/sata devices like hard drives and optical drives. lsusb - This command shows the USB controllers and details about devices connected to them. By default brief information is printed. Use the verbose option "-v" to print detailed information about each usb port. inxi -Fx - Inxi is a 10K line mega bash script that fetches hardware details from multiple different sources and commands on the system, and generates a beautiful looking report that non technical users can read easily. lsblk - List out information all block devices, which are the hard drive partitions and other storage devices like optical drives and flash drives. df -H - Reports various partitions, their mount points and the used and available space on each. pydf - An improved df version written in python, that displays colored output that looks better than df. sudo fdisk -l - Fdisk is a utility to modify partitions on hard drives, and can be used to list out the partition information as well. mount | column -t - The mount is used to mount/unmount and view mounted file systems. free -m - Check the amount of used, free and total amount of RAM on system with the free command. # display information about the processor/cpu $ sudo dmidecode -t processor # memory/ram information $ sudo dmidecode -t memory # bios details $ sudo dmidecode -t bios - The dmidecode command is different from all other commands. It extracts hardware information by reading data from the SMBOIS data structures (also called DMI tables). === 15. /proc files === Many of the virtual files in the /proc directory contain information about hardware and configurations. Here are some of them CPU/Memory information # cpu information $ cat /proc/cpuinfo # memory information $ cat /proc/meminfo Linux/kernel information $ cat /proc/version Linux version 3.11.0-12-generic (buildd@allspice) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu7) ) #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 SCSI/Sata devices $ cat /proc/scsi/scsi Attached devices: Host: scsi3 Channel: 00 Id: 00 Lun: 00 Vendor: ATA Model: ST3500418AS Rev: CC38 Type: Direct-Access ANSI SCSI revision: 05 Host: scsi4 Channel: 00 Id: 00 Lun: 00 Vendor: SONY Model: DVD RW DRU-190A Rev: 1.63 Type: CD-ROM ANSI SCSI revision: 05 Partitions $ cat /proc/partitions major minor #blocks name 8 0 488386584 sda 8 1 73400953 sda1 8 2 1 sda2 8 5 102406311 sda5 8 6 102406311 sda6 8 7 1998848 sda7 8 8 208171008 sda8 11 0 1048575 sr0 === 16. hdparm === The hdparm command gets information about sata devices like hard disks. $ sudo hdparm -i /dev/sda /dev/sda: Model=ST3500418AS, FwRev=CC38, SerialNo=9VMJXV1N Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs RotSpdTol>.5% } RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=4 BuffType=unknown, BuffSize=16384kB, MaxMultSect=16, MultSect=16 CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=976773168 IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120} PIO modes: pio0 pio1 pio2 pio3 pio4 DMA modes: mdma0 mdma1 mdma2 UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6 AdvancedPM=no WriteCache=enabled Drive conforms to: unknown: ATA/ATAPI-4,5,6,7 * signifies the current active mode ===== Remove symlink ===== $ unlink mySymLink - use the "unlink" command and make sure not to have the / at the end ===== Troubleshooting site that are down ===== ssh -vvvi ~/.ssh/id_rsa ubuntu@52.10.24.217 - run an ssh with the verbosity flags on nping, nc, tcptraceroute, and tcpdump - run end-to-end connection testing ===== Generate a SSH Key===== ssh-keygen eval `ssh-agent` ssh-add ~/.ssh/id_rsa cat ~/.ssh/id_rsa.pub | xclip -sel clip ===== Installing .deb files ===== sudo dpkg -i /path/to/deb/file ===== how to use html2text for wiki ===== html2text -o output.txt input.html ===== Helpful git script ===== Run the following from the parent directory, ''plugins'' in this case: find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \; To clarify: * ''find .'' searches the current directory * ''-type d'' to find directories, not files * ''-depth 1'' for a maximum depth of one sub-directory * ''-exec {} \;'' runs a custom command for every find * ''git --git-dir={}/.git --work-tree=$PWD%%/{%%} pull'' git pulls the individual directories To play around with find, I recommend using ''echo'' after ''-exec'' to preview, e.g.: find . -type d -depth 1 -exec echo git --git-dir={}/.git --work-tree=$PWD/{} status \; Note: if the ''-depth 1'' option is not available, try ''-mindepth 1 -maxdepth 1''. find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \; find . -type d -maxdepth 1 -exec echo git status \; - for updating multiple git repos in the current folder ===== How to kill a process in linux ===== ps aux - use this to list all of the current running processes ps aux | grep "chrome" - use this to search the processes for "chrome" USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 193540 9780 ? Ss May16 3:24 /sbin/init splash root 2 0.0 0.0 0 0 ? S May16 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< May16 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< May16 0:00 [rcu_par_gp] root 6 0.0 0.0 0 0 ? I< May16 0:00 [kworker/0:0H-kblockd] - output looks like this kill -9 [pid] - use this to kill the process, the ''-9'' will kill all processes that it can kill, including the tree ===== Linux Time ===== timedatectl set-local-rtc 1 --adjust-system-clock timedatectl timedatectl set-local-rtc 0 --adjust-system-clock - use the first command to set linux to use local time, use the second command to check the settings for time, and use the third command to return it to default