Random Useful Linux Command Notes

Why am I making this seemly random note?

This is a random useful set of Linux (Ubuntu, CentOS, and other Linux variants) commands that I use on a daily basis. Some of the commands may not be correct as I sometimes rush to add to the list. If you found this page and see a better way or discrepancy, please comment below or message me with the correct fix or improvement.

Temporary block single IP

# iptables -L     <--- Lists the current rules
# iptables -I INPUT -s  <IP> -j DROP
# iptables -L

How do I do a Linux Copy file with keeping permissions and ownership

# sudo rsync -a /var/www/onesite.com/ 
# /var/www/toanother.com/

How to do a TAR Backup

tar -zcvf archive-name.tar.gz directory-name
Where,
-z: Compress archive using gzip program
-c: Create an archive
-v: Verbose i.e display progress while creating an archive
-f: Archive Filename

For example, you have a directory called /home/bill/prog and you would like to compress this directory then you can type tar command as follows:

# tar -zcvf code-nov-2019.tar.gz /home/somedir/prog

The above command will create an archive file called code-nov-2019.tar.gz in the current directory.
If you wish to restore your archive then you need to use following command (it will extract all files in current directory):

$ tar -zxvf code-nov-2019.tar.gz

Where,
-x: Extract files

If you wish to extract files in particular directory, for example in /tmpdir, then you need to use the following command:

$ tar -zxvf code-nov-2019.tar.gz -C /tmpdir
$ cd /tmpdir
$ ls -al

This was a nice site that helped me with how to compress a while Linux or Linux dir
http://www.cyberciti.biz/faq/how-do-i-compress-a-whole-linux-or-unix-directory/

How to View Current Status of Startup Services

# chkconfig --list | grep 3:on

Web directory settings

chmod -R 775 *
chmod -R a+X *

System NTP Date Cron:

# sudo vi /etc/cron.daily/ntpdate
# !/bin/sh
# ntpdate ntp.ubuntu.com

Example of copy directory:

# sudo scp -r -i ~/.ssh/genkey.pem ./parked/ec2user@<ip-address>:/home/ec2user/nodeapps/somedirectory/

Get just the file names:

# ls | xargs -n1 basename >> result.txt

Testing REST services

# curl -i X GET http://site.com/users
# curl -i X DELETE http://site.com/users/<id_to_delete>
# curl -i X POST -H 'Content-Type:application/json' -d '{"name":"User Name", "age":"20"}' http://site.com/users
# curl -i X PUT -H 'Content-Type:application/json' -d '{"name":"User Name", "age":"20"}' http://site.com:3000/users/<id_to_modify>

Setting public_html permissions correctly:

# chmod -R a+rX public_html

Secure Copy from MAC to EC2/Linus server

# sudo scp -i ~/.ssh/genkey.pem ./from_file_name ec2user@<ip-add>:/home/ec2user/originals
# curl -o /tmp/ec2-api-tools.zip http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip

To see Apache:

# cd  /etc/httpd/conf
# vi httpd.conf
# /etc/init.d/httpd restart
# ps aux | grep apache

Setting Owners Globally:

# chown -R <user>:<group> ./<directory>

Add site to Apache:

# vi /etc/httpd/conf/httpd.conf

Restart Apache

# /etc/init.d/httpd restart

Edit Tomcat worker properties for site redirect to Tomcat

# vi /etc/httpd/conf/workers.properties

Remove recursive directory without asking:

# rm -rf <dir>

Adding a user:

# useradd  <username> -m -g<groupname> -p<password> -c<comments>

Force the .bash_profile to execute:

# source ~/.bash_profile

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: