Linux/Bash
 
 
<command> --help
man <command>
 
Run a bash script:
("." refers to current directory)
./breezeway.sh 'request ue detach-ue imsi 001001000033765'
bash breezeway.sh 'request ue detach-ue imsi 001001000033765'
 
Who's logged on:
who
 
View SSH logins:
cat /var/log/secure
 
Find a file:
locate CGI.pm 
 
Linux Version:
cat /etc/*-release
cat /etc/redhat-release
cat /proc/version

Apache Version:
httpd -v
apachectl -V

MySQL Version:
mysql -V
 
MySQL Command Line Options:
 
Perl Version:
perl -v
 
Current kernel version:
uname -r
 
Others:
uptime
last reboot
who -b
yum history
 
 
---------------------------------------
Shift + PageUp
Shift + PageDown
 
Ctrl + E - go to end
Ctrl + G - go to end
CTRL-l : Clear the screen
CTRL-c : Cancel command.
CTRL-a : Moves cursor to beginning of line. 
CTRL-e : Moves cursor to end of line.
 
Less:
Page Up
Page Down
G - end of file
g - beginning of file
 
In man pages:
f - forward one window
b - back one window
q - exit
 
 
---------------------------------------
Linux distribution name/version:
cat /etc/*-release
lsb_release -a 
 
 
 
---------------------------------------
 
Disk space usage: df -h
Available memory: free -m
Detailed memory info: cat /proc/meminfo
Processing power: cat /proc/cpuinfo
 
List Directory Sizes:
du --max-depth=1 /home/michaelt/public_html/wp-content | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}' 
 
Count files in directory:
ls -l /home/wyogaor/mail/wyoga.org/wyoga/cur | wc -l
 
---------------------------------------
---------------------------------------
 
Display all files and folders (in /home) sorted by MegaBytes:

 

du --max-depth=1 /home | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}'
 
---------------------------------------
--------------------------------------- 
 
Get number of directories:
 
ls -l /home/ | grep -c ^d
---------------------------------------
 
cd ../home/catalyst/icdf

pwd (print working directory)

ls (list directory contents)

ls -alh (all, long listing format, human-readable)

ls -d */ (list directories only)

grep (globally search a regular expression and print)
$ grep apple fruitlist.txt
$ grep "mysqldump" .bash_history
$ grep PassivePortRange /etc/pure-ftpd.conf
grep -rnw 'directory' -e "pattern"
grep --include=\*.{php,config} -rnw 'directory' -e "pattern"
-r [recursive]
-n [line numbers]
-w [whole word match]

find
find /home/ -name horde.sql
find . -type f \( -name "*.class" -o -name "*.sh" \)
find . -type f \( -name "configure.php" -o -name "configuration.php" \)
http://www.hypexr.org/linux_find_help.php
 
 
To find the 10 most recently modified files, sorted in the reverse order of update time (most recent first):
find /some_dir -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | head -n 10


To search for files in /some_dir and all its sub-directories that have been modified in the last 2 days:
find /some_dir -type f -mtime -2 -printf '%TY-%Tm-%Td %TT %p\n'


Specify range. To search for files in /some_dir and all its sub-directories that have been modified in the last 7 days, but not in the last 3 days:
find /some_dir -type f -mtime -7 ! -mtime -3
find /some_dir -type f -mtime -7 ! -mtime -5 -printf '%TY-%Tm-%Td %TT %p\n'
OR (same result)
find /some_dir -type f -mtime +3 -mtime -7


To search for files in /some_dir (and all its sub-directories) that have been modified in the last 60 minutes, and print out their file attributes:
find /some_dir -type f -mmin -60 -exec ls -lah {} \;
 
 

NOTE: "find" is a file locator while "grep" searches for patterns within a file.


cat (catenate - reads data from files and outputs contents)
cat file1.txt file2.txt

tac filename | less (read file backwards)

less filename
G - end of file
g - beginning of file
/ - search forward
? - search backward

cp (copy files and directories)

cp -R report/ reports/w\ (recursive)

chown (changes the owner and owning group of files)
 
chown root:root somefile

chown -R root:friends somefile (recursive)

chgrp (Changes group ownership of a file or files)

chgrp -R www-data reports/
 
chmod  744 ~/yada

chmod -R 744 ~/yada  (-R = recursive)
 
 
 
User                        Group                     Other
r      w      x              r      w      x               r      w      x
4      2      1              4      2      1               4      2      1
 
 
COMMON PERMISSIONS:

-rw------- (600) — Only the owner has read and write permissions.

-rw-r--r-- (644) — Only the owner has read and write permissions; the group and others can read only.

-rwx------ (700) — Only the owner has read, write and execute permissions.

-rwxr-xr-x (755) — The owner has read, write and execute permissions; the group and others can only read and execute.

-rwx--x--x (711) — The owner has read, write and execute permissions; the group and others can only execute.

-rw-rw-rw- (666) — Everyone can read and write to the file. (Be careful with these permissions.)

-rwxrwxrwx (777) — Everyone can read, write and execute. (Again, this permissions setting can be hazardous.)

Here are some common settings for directories:

drwx------ (700) — Only the user can read, write in this directory.

drwxr-xr-x (755) — Everyone can read the directory, but its contents can only be changed by the user.
 
drwx--x--x (711) —  cPanel user directories (/home/<username>
 


mysqldump xxxxxDBNAMExxxxx > xxxxxxxxxx.sql
mysql -u root xxxxxDBNAMExxxxx < databases/xxxxxxxxxx.sql
mysql database_name < database_name.sql
 
--------------------------------------- 
 
Create an empty file:
touch filename.txt
 
Create a file with date string:
touch filename_"`date +"%Y_%m_%d"`".txt
mysqldump icdf | gzip > "/home/catalyst/DATA/icdf_db_"`date +"%Y_%m_%d"`".sql.gz"
 
"%" must be escaped in cron jobs:
mysqldump icdf | gzip > "/home/catalyst/DATA/icdf_db_"`date +"\%Y_\%m_\%d"`".sql.gz"

BUT - if backslashes added to ordinary command, they become part of the name (e.g. "icdf_db_TEST\2017_\08_\28.sql.gz")
 
---------------------------------------
 
Vi Editor:
vi mytest.txt
- Type "i" to enter editing mode.
- Type text.
- Press "Esc" to return to command mode.
- Type ":wq" to save and close. 
 
--------------------------------------- 
 

Viewing, copying, moving and deleting files

ls

ls -l

Display the contents of the current directory

ls -a

ls -ltr

ls -ld /lib/*/

Display also hidden files and hidden directories

Order files by last modified date

Directories only

 

 

cp filename /path/dir_name

Copy filename into directory /path/dir_name

cp -r dir_name /path/dir_name2

Copy the entire dir_name into /path/dir_name2

cp filename1 filename2 /path/dir_name

Copy filename1 and filename2 into /path/dir_name

 

 

rm name

Remove a file or directory called name

rm -r name

Remove an entire directory as well as its included files and subdirectories

 rm -rf name  Remove an entire directory as well as its included files and subdirectories w/o prompts for all the files

 find /home/modelsig/mail/new -type f -delete

 Remove a ton of files to get around "too many arguments" problem

mv filename /path/dir_name

Move filename into /path/dir_name

mv filename1 filename2

Rename filename1 to filename2

 

 

cat filename

Display filenames contents

 

 

more filename

Display filename in pages. Use spacebar to view next page

   
less filename  Better than more
tac filename | less  Read from end of file

 

 

head filename

Display filenames first 10 lines

head -15 filename

Display filenames first 15 lines

 

 

tail filename

Display filenames last 10 lines

tail -15 filename

Display filenames last 15 lines

tail -f myfile.txt
 Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates

 

 

pwd

Display current directory

 

 

cd /path/dir_name

Change to directory /path/dir_name

cd ..

Go 1 directory up

 

 

mkdir dir_name

Create directory dir_name

rmdir dir_name

Delete directory dir_name

 

Finding files and text within files

updatedb

Update (create first time used) a database of all files under the root directory /

locate filename

locate crontab

Find file filename searching in the database

All files that contain the word crontab

 

 

find / -name filename

Starting from the root directory search for the file called filename

find / -name *filename

 

find -iname "MyCProgram.c"

 

find -iname "MyCProgram.c" -exec md5sum {} \;

Same as above but search for file containing the string filename

 

Case insensitive find

 

Execute command on files found

 

 

grep string /path/dir_name

 

grep -i "the" demo_file

 

grep -A 3 -i "example" demo_text

 

grep -r "ramesh" *

 

Starting from /path/dir_name search for all files containing string

 

Case-insensitive

 

Print matched line + 3 lines after

 

Search for a given string in all files recursively

 

 

which application_name

Search $path for application app_name

whereis application_name

Search $path, man pages and source files for application_name

 

Archived files

Decompress

tar -xzf filename.tgz

Decompress tzg file

tar -xzf filename.tar.gz

Decompress tar.gz file

tar -xjf filename.tar.bz2

Decompress tar.bz2 file

 

Compress

tar -czf filename.tar /path/dir_name

Compress directory /path/dir_name to filename.tar

tar -czvf compressed_file.tar.gz file1.txt file2.jpg

Compress multiple files

gzip -c filename > filename.gz

Compress /path/dir_name to filename.tar.gz

bzip2 -c filename > filename.bz2

Compress /path/dir_name to filename.tar.bz2

 

Using rpm files 

rpm -hiv package.rpm

Install rpm called package.rpm

rpm -hiv --force package.rpm

Install rpm called package.rpm by force

rpm -hUv package.rpm

Upgrade rpm called package.rpm

rpm -e package.rpm

Delete rpm called package.rpm

rpm -qpil package.rpm

List files in not-installed rpm called package.rpm

rpm -ql package.rpm

List files in installed rpm called package.rpm

rpm -q str

List installed rpms containing the string str

rpm -qf /path/application_name

Display the rpm that contains application application_name

 

Starting and Stopping 

startx

Start the X system

shutdown -h now

Shutdown the system now and do not reboot

halt

Same as above

shutdown -r now

Reboot

reboot

Same as above

shutdown -r +10

Reboot in 10 minutes

shutdown -c

Cancel a pending shutdown

at 3:00 AM tomorrow
at> echo "shutdown -r now"
ENTER
Control-D


atq (to display the queue)
atq
434     Fri Nov  9 15:25:00 2018 a root


atrm deletes jobs, identified by their job number.
atrm 434

 Schedule a reboot using the at command.

The at command schedules a command to be run once at a particular time that you normally have permission to run. The at command can be anything from a simple reminder message, to a complex script. You start by running the at command at the command line, passing it the scheduled time as the option. It then places you at a special prompt, where you can type in the command (or series of commands) to be run at the scheduled time. When you're done, press Control-D on a new line, and your command will be placed in the queue.

$ at now + 1 minutes
at> echo "Hello Fido" > test.txt

at 9:30 PM Tue
at noon
at midnight
at tomorrow (24 hrs. from now)
at 2:30 PM tomorrow
at 2:30 PM 10/21/2014

 Restart Service
/etc/init.d/service restart 
/usr/local/cpanel/scripts/restartsrv service
systemctl restart service-name.service
 
.........

/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart

service mysqld start
service mysqld stop
service mysqld restart

service mysql start
service mysql stop
service mysql restart

  

Mounting filesystems

mount -t vfat /dev/sd(a)(1) /mnt/c_drive

Mount the first partition 1 of the first hard disk drive a which is in fat32 vfat dormat under /mnt/c_drive directory

mount -t iso9660 /dev/cdrom /mnt/cdrom

Mount cdrom under /mnt/cdrom directory

 

 

umount /mnt/hda1

Unmout the above

 

User administration

users

Display users currently logged in

adduser username

Create a new user called username

passwd username

Define password for user called username

who

List logged-in users

 List users  less /etc/passwd
 List users name only  awk -F':' '{ print $1}' /etc/passwd
 who -b  Last reboot
 last reboot  List of last reboots
 last root  list of past logins for user "root"

whoami

Display current user

finger username

Displays info about user username

su

Log in as root from current login

su -

Log in as root from current login and take root's path

exit

Exit from console login (ie, logout).

 

Processes 

command

Execute command in the foreground

command &

Execute command in the background

ctrl+z

Suspend a program

ctrl+c

Interrupt a program

ps

List all processes

kill -9 pid

Kill process with id pid

top

Monitor processes in real time

 

Networking 

hostname

List the system's hostname

ifconfig

Set/Display network information

host ip

Resolves ip's hostname

ping ip/hostname

Check if ip/hostname is reachable

traceroute ip/hostname

Find network path to ip/hostname

   


 

System Information

uname -a

General system information

fdisk -l

List partition tables

cp filename /path/dir_name

Copy filename into directory /path/dir_name

df -T -h

List filesystem disk space usage

lspci

List PCI devices

lsusb

List USB devices

free -m

Display RAM+Swap usage

 

Program Compile

gcc -o output file.c

Compile a C program

./output

Run a C program you have compiled

g++ -o output file.cpp

Compile a C++ program

./output

Run a C++ program you have compiled

./configure && make && su -c 'make install'

Configure, compile and install a program with Makefile

 

 

CRONTABS

https://www.computerhope.com/unix/ucrontab.htm
https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
https://serverfault.com/questions/801257/should-i-edit-etc-crontab-or-run-crontab-e-as-root
https://www.thegeekstuff.com/2009/06/15-practical-crontab-examples

https://www.perl.com/article/43/2013/10/11/How-to-schedule-Perl-scripts-using-cron/

***************************

When you create a crontab file, it is automatically placed in the /var/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root, if you have superuser privileges.

System crontabs: etc/crontab; etc/cron.daily; etc/cron.weekly; etc.

***************************


LOAD CRONTAB FROM TEXT FILE:
crontab /home/catalyst/icdf/script/cron-file.txt

MAILTO=webmaster@wyoming.com
# First day of every month at 5am:
0 5 1 * * perl /home/catalyst/icdf/script/ContractExpireNotify.pl

-------------------------------------------

ROOT USER:
crontab -l

crontab -u michael.carroll -l
crontab /var/spool/cron/crontabs/www-data
sudo crontab -l -u jeff

Minute - Hour - Day - Month - Day(s) of Week

# Every minute:
* * * * * /home/catalyst/icdf/script/SendMailPerl.pl
# * * * * * /home/catalyst/icdf/script/ContractExpireNotify.pl
#
# Every 10 minutes:
# */10 * * * * /home/ramesh/check-disk-space
#
# Every day at 5am:
# 0 5 * * * /home/catalyst/icdf/script/ContractExpireNotify.pl
#
# Send the job output to a log file:
# 0 8 * * * /usr/local/bin/myjob > /var/log/myjob.log 2>&1

crontab -e
TO EXIT EDITOR:
:x<Enter> quit vi, writing out modified file to file named in original invocation
:wq<Enter> quit vi, writing out modified file to file named in original invocation
:q<Enter> quit (or exit) vi
:q!<Enter> quit vi even though latest changes have not been saved for this vi call

 

LIST USER CRONTABS:
ls /var/spool/cron
 

VIEW ALL CRONTABS:
more /etc/crontab


VIEW SINGLE USER CRONTAB:
crontab -u johnvinc -l


CRONTAB LOGS:
grep CRON /var/log/syslog
tail -10 /var/log/cron
cat /var/log/cron | grep johnvinc
head -10 /var/log/cron | grep johnvinc


REMOVE CRONTAB:
crontab -u johnvinc -r

RESTART CRON:
/etc/init.d/cron reload

Testing Cron Jobs By Simulating Cron Shell:
https://cronitor.io/docs/cron-troubleshooting-guide

# env -i /bin/sh
# cd ~
# env
PWD=/root

- Then run the script to see if there's a useful error message.

# /usr/local/bin/Libre_ap_report_mail.pl monthly
Can't locate Mail/Sender.pm in @INC (you may need to install the Mail::Sender module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/local/bin/Libre_ap_report_mail.pl line 6.
BEGIN failed--compilation aborted at /usr/local/bin/Libre_ap_report_mail.pl line 6.

- Exit, then run # env again to see if there is something missing in the bare shell (/bin/sh).

- Might be able to do a fix on scripts like this:

use lib '/root/perl5/lib/perl5';

 

 

 

Give full access to user and group (i.e read, write and execute ) on a specific file.

$ chmod ug+rwx file.txt

 

Revoke all access for the group (i.e read, write and execute ) on a specific file.

$ chmod g-rwx file.txt

 

Apply the file permissions recursively to all the files in the sub-directories.

$ chmod -R ug+rwx file.txt

---------------------------------------

LESS:

$ less huge-log-file.log

CTRL+F – forward one window

CTRL+B – backward one window

---------------------------------------

$ mysql -u root -p

$ mysql -u root -pXXXXPWXXXXX

 

echo "Some Text..." > myfile.txt

echo "Some Text..." >> myfile.txt

 

q - to quit out of man page

 
 
---------------------------------------
To view current processes:
ps
ps aux | less
ps aux | grep "process-name"
ps aux | grep "httpd"
ps alx | grep "mysqld"

---------------------------------------
 
Putty Text Colors:

* Executable files: Green
* Normal file : Normal
* Directory: Blue
* Symbolic link : Cyan
* Pipe: Yellow
* Socket: Magenta
* Block device driver: Bold yellow foreground, with black background
* Character device driver: Bold yellow foreground, with black background
* Orphaned syminks : Blinking Bold white with red background
* Missing links ( … and the files they point to) : Blinking Bold white with red background
* Archives or compressed : Red (.tar, .gz, .zip, .rpm)
* Image files : Magenta (.jpg, gif, bmp, png, tif)
 
---------------------------------------