Posts

Showing posts from April, 2020

Linux Commands — list directory options

list directory contents ls -lrta -a, --all : do not ignore entries starting with . -l :use a long listing format -r, --reverse: reverse order while sorting -t :sort by modification time To be continued…

VirtualBox commands

Here are few frequently used commands Creating a VM VBoxManage createvm — name ubuntu1804 — ostype Ubuntu_64 — register To check the info of the VM created VBoxManage showvminfo ubuntu1804 Setting Ram for VM VBoxManage modifyvm ubuntu1804 — memory 4096 — boot1 dvd — vrde on — vrdeport 5001 Create a bridge virtual network adapter VBoxManage modifyvm ubuntu1804 — nic1 nat Add a storage controller to be used with the hard disk VBoxManage storagectl ubuntu1804 — name ubuntu1804_SATA — add sata Create a new virtual hard disk image VBoxManage createhd — filename ubuntu1804.vdi — size 10280 — format VDI — variant Standard Attach the hard disk to the controller? VBoxManage storageattach ubuntu1804 — storagectl ubuntu1804_SATA — port 1 — type hdd — medium ubuntu1804.vdi Attach the dvddrive to the controller? VBoxManage storageattach ubuntu1804 — storagectl ubuntu1804_SATA — port 0 — type dvddrive — medium /home/guest1/iso/ubuntu-18.04.1-desktop...

Create custom commands in Linux (aka alias)

Image
Let’s say you are using some of the commands frequently and you don’t want to type the whole command everytime. An alternative is to create a short command for it and use that instead. First, go to the home folder in the terminal which looks like the following /home/username In case if you are in some other directory, you can type  ~  in terminal and press Enter to get there Next, you can list the contents of that directory including the hidden files using ls -a Here, -a is to show hidden files which start with dot Among the list of files, find  .bashrc  and open it At the end of the file, add some custom commands by using alias. alias [custom command name]=’[original command] Here is an example. You can create any number of custom commands here After entering the custom commands, close the terminal and open a new one.

List files in directory using Python

Image
First  import os Then call  listdir()  method as follows In terminal, first we have listed the directories. Later, we have used python to list them This is another example using Jupyter

Enable Clipboard in VirtualBox

Image
1. Open VirtualBox 2. Select the GuestOS instance where you want to enable it. 3. Right click on it and select Settings (Or click on the Settings icon above) 4. In  General , select  Advanced tab  and enable Bidirectional for  Shared Clipboard  as below. You are all set now :)

Passing a file location to python

Rather than hardcoding paths in your Python script we should make use of the path operation from module OS os.path.expanduser( path )  expands the path to the user’s home directory os.path.join( path1 ,*path2*,…)  joins path elements with the appropriate separator os.sep  gives the OS dependent path separator ( /  for Linux/Unix,  \  for Windows) os.getcwd()  gives the current working directory os.path.abspath(path)  gives the OS dependent absolute path of a given path Example: >>>import os >>>path = os.path.join(os.path.expanduser('~'), 'documents', 'python', 'file.txt') >>>print (path) Result /home/user/documents/python/file.txt ## when on Ubuntu C:\Users\user\documents\python\file.txt ## when running on Windows Source link:  https://askubuntu.com/users/3940/takkat

Ports on Ubuntu — Netstat

List all listening ports sudo netstat -plnt Filter by a particular port $ sudo netstat -plnt | grep ':80' If something else is listening on the port, you can disable the program by running  sudo service httpd stop or change its configuration so that it no longer listens on the port. When  netstat  shows the port is free, enable the correct service (for example  sudo service vsftpd start ). If you make any changes because the incorrect service is listening, run the  netstat  command again. If  netstat  doesn’t show the program listening on the correct port, you need to address its configuration before you go any further. Check status of firewall sudo ufw status verbose To allow/open a port 21 sudo ufw allow 21/tcp More:  https://www.cyberciti.biz/faq/how-to-open-firewall-port-on-ubuntu-linux-12-04-14-04-lts/

Ubuntu 18 Lan/Wifi settings

Navigate to /etc/netplan/01-netcfg.yaml Apply necessary changes and save using the command sudo netplan apply

Linux logs

To access the system logs though command line, try the following cat /var/log/syslog or sudo cat /var/log/syslog

Linux History

Image
Use the following command to check the list of commands given earlier. history You can change the size(Number of commands) of the history. To check the current size. Give the following The maximum number of commands to remember on the history list. echo $HISTSIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE

Raspberry Pi setup

Image
1 — Download the iso file You can download the iso from the following link. Currently (Oct 2019) there are 3 versions available. Download Raspbian for Raspberry Pi Raspbian is the Foundation's official supported operating system. You can install it with NOOBS or download the image… www.raspberrypi.org Depending on the size of microSD card, and your requirements, pick the corresponding iso and download it. 2 — Burn the image into the microSD card For Mac users (I have downloaded into Downloads folder) Check the drives before inserting the microSD card using the following command df -h Now check the drives after inserting the microSD. You will see an additional drive. (Example: /dev/disk3. here the disk-number is 3) Unmount the microSD card. You can go to disk utility tool and click on corresponding external drive and click on unmount Click on Unmount on Disk Utility Now burn the ios using the following command sudo...

Bash: Multi line comment using vim

For instance, you want to comment from line 1 to 3, try the following :1,3s/^/# Here, you are saying from line 1 to 3 (1,3) to substitute(s) the beginning of the line (^) with the character #

ssh configuration file

Image
You can login into host using ssh command through command line. Instead of typing the host details everytime you login, you can store them under the following file ~/.ssh/config Use the following format in the config file: ssh config example The ssh client receives its configuration in the following precedence order: Options specified from the command line Options defined in the  ~/.ssh/config Options defined in the  /etc/ssh/ssh_config

Access Raspberry pi using usb

Image
Connect to USB (not PWR) using USB data cable (not charging cable) After installing rasbian, edit the following files 1 Enable SSH by creating the file using the following command touch /Volumes/boot/ssh 2. Edit config.txt file vim /Volumes/boot/config.txt add the following line at the end of the file dtoverlay=dwc2 3. Edit cmdline.txt vim /Volumes/boot/cmdline.txt after the word “ rootwait ” add the following text with one space modules-load=dwc2,g_ether 4. Connect using micro usb to your mac and ssh into it.

Run script from anywhere in linux terminal

When we have a  custom  script in a particular directory, we navigate to that directory to run the script. We can place those scripts under   /usr/local/bin  which is a suitable for widely used commands not provided by the system. System  commands/scripts on the other hand have specific directories like /usr/bin /bin etc..

Create a Systemd service in Linux

Most of us are familier with applications in Windows like Word, Notepad etc. To use a Word application, it is not enough to install it. After the installation, we open (start) a Word application, use it and then later close it. Similarly, in Linux, we can treat similar applications like services. We can do several operations like start, status, restart and stop etc. Let us create a simple service. a) Create a script (bash) Open terminal and create a script named hello.sh vim hello.sh Now, let’s create a bash file which prints some text. ( under /home/user/test ) #!/bin/bash while : do echo "test service.. mike testing" sleep 10 done Give executable permission to this file chmod +x /home/user/test/hello.sh b) Create a service file Under /etc/systemd/system/ , create a service file vim hello.service We have 3 sections when creating a service file  1) Unit 2) Service 3) Install. For now, we shall just go with the mandatory one. ...

Copy files from one computer to other using ‘scp’ command

Image
copy files between computers using scp cmd scp file.txt remote_username@10.10.0.2:/remote/directory From the computer which has the source file, go to the command line and, provide the path of the source file followed by scp and then, username@hostname of the remote computer and then, the path where you want it to be copied on remote computer. Another example: Default port number is 22. So we generally use -P in case we have to explicitly specify any port other than 22 like below scp -P 2000 Downloads/simple2.html pi@raspberrypi.local:~/learning/python/webscraping Copy local directory to remote computer scp -r localDirectory user@remotecomp:/path-to-destination Working example: scp -r capturing-images pi@raspberrypi.local:/home/pi/Downloads Copy from Pi to your Mac scp user@remote:path-to-file . Dot at the end means to copy to the current directory of the mac Working examples: (Run from Mac terminal) a) Copy img1.jpeg file from remote to...