Sunday, December 9, 2018

Useful linux commands



1. SSH to remote server


ssh -i <PEM FILE> <REMOTE_USERNAME>@<REMOTE_IP>


ex: ssh -i ~/.ssh/admin-user.pem admin-user@10.10.15.20

2. Copy a Remote File to a Local System using the scp Command


scp -<REMOTE_USERNAME>@<REMOTE_IP>:<FULL_PATH_TO_REMOTE_FILE> <PATH_OF_LOCAL_SYSTEM>

ex: scp remote_username@10.10.0.2:/remote/file.txt /local/directory

3. Copy a File Between Two Remote Systems using the scp Command


scp  <REMOTE1_USERNAME>@<REMOTE1_IP>:<FULL_PATH_TO_REMOTE1_FILE> <REMOTE2_USERNAME>@<REMOTE2_IP>:<FULL_PATH_TO_REMOTE2_FILE>

ex: scp user1@host1.com:/files/file.txt user2@host2.com:/files
In here, file.txt in host1 will be copied to host2 files directory.

4. Compress a folder

zip -r <ZIP_FILE_NAME> <FOLDER_NAME>

ex: zip -r abc.zip abc
In here, “abc” folder will be compressed to “abc.zip”


5. Extract a zip file


unzip -r <ZIP_FILE_NAME> <FOLDER_NAME>
ex: unzip file.zip -d destination_folder


Note: If unzip command is not installed in your system, you can use below command to install unzip.


sudo apt-get install unzip


6. Create a symlink

ln -s <PATH_TO_FILE> <PATH_TO_SYMLINK>

ex: ln -s /path/to/file /path/to/symlink


7 .Find all files containing specific text

grep -rnw '<PATH_TO_FOLDER>' -e '<PATTERN>'

ex: grep -rnw '/path/to/somewhere/' -e 'pattern'
Note:
  • -r or -R is recursive
  • -n is line number,
  • -w stands for match the whole word.


8. Replace a string in multiple files

sed -i 's/<OLD_WORD>/<NEW_WORD>/g' *

ex : sed -i 's/foo/bar/g' *
In here, occurrences of "foo" will be replaced with "bar".

Note: Before replace, go to the specific folder using "cd" command.
  • cd /path/to/your/folder