Upload files to FTP server using cURL

To upload a file to an FTP server with cURL is very simple. We can achieve that by using the below commands -T option in cURL.

We can achieve that by using the cURL in the following manner:
curl -T $FILE -u $USERNAME:$PASSWORD $SERVER/$DIR
where,
$FILE : filename which you want to upload
$USERNAME : username of the FTP server
$PASSWORD : password of FTP server
$SERVER : server URL
$DIR : Directory on server in which you want to upload

From the man page of cURL:

-T, --upload-file 
       This transfers the specified local file to the remote URL. If there is no file part in the specified URL, Curl  will
       append  the  local file name. NOTE that you must use a trailing / on the last directory to really prove to Curl that
       there is no file name or curl will think that your last directory name is the remote file name  to  use.  That  will
       most likely cause the upload operation to fail. If this is used on an HTTP(S) server, the PUT command will be used.

       Use  the file name "-" (a single dash) to use stdin instead of a given file.  Alternately, the file name "." (a sin‐
       gle period) may be specified instead of "-" to use stdin in non-blocking mode to allow reading server  output  while
       stdin is being uploaded.

       You  can  specify one -T for each URL on the command line. Each -T + URL pair specifies what to upload and to where.
       curl also supports "globbing" of the -T argument, meaning that you can upload multiple files  to  a  single  URL  by
       using the same URL globbing style supported in the URL, like this:

       curl -T "{file1,file2}" http://www.uploadtothissite.com

       or even

       curl -T "img[1-1000].png" ftp://ftp.picturemania.com/upload/

Hope it helps 🙂

Advertisements

Update a forked repository

To update our forked repository with the latest changes in the main repository (upstream) we need to follow the following methods:
Step – 1: Add the remote repository URL,
git remote add upstream
Step – 2: Fetch all the branches from upstream branch
git fetch upstream
Step – 3: Checkout to the master branch
git checkout master
Step – 4: Rebase it
Rewrite your master branch so that any commits of yours that aren’t already in upstream/master are replayed on top of that other branch:
git rebase upstream/master

Now your local repo will contain all the changes from the upstream repo.
To update your forked repo on github, just push this changes to your forked repo using the command:
git push origin master

Now your local as well as forked repo will be updated with the new changes.

Merging two git commits into one

Here, we will see methods to merge two git commits into one for a clean history.

In the below example:
We have added two files in two different commit:
File a.txt – in commit b7d542d
File b.txt – in commit 06c23f5
Now we will merge this two commits to appear as one with meassage ‘Added file a.txt and b.txt’

Firstly, we need to select those latest commit for rebase using the below command:
git rebase -i HEAD~2

This will open up an editor.
[Optional] To define which editor to to use for editing, use command git config --global core.editor
For Atom you can do,
git config --global core.editor "atom --wait" # –wait option is to make editor wait for the window to be closed before returning
For VSCode,
git config --global core.editor "code --wait"
For Sublime,
git config --global core.editor "subl -n -w"

In the editor it will show something like:

pick 06c23f5 Added file b.txt
pick b7d542d Added file a.txt

# Rebase 6d085c2..b7d542d onto 6d085c2 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

From the comments in the editor, we can use any of the squash or fixup commands accoding to our need and modify the text in the editor to look like:

pick 06c23f5 Added file b.txt
squash b7d542d Added file a.txt

# Rebase 6d085c2..b7d542d onto 6d085c2 (2 command(s))
#
.....

It will open up one another editor, with text like:

# This is a combination of 2 commits.
# The first commit's message is:

Added file a.txt

# This is the 2nd commit message:

Added file b.txt

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Sep 21 12:09:18 2017 +0530
#
# interactive rebase in progress; onto 6d085c2
# Last commands done (2 commands done):
#    pick 06c23f5 Added file e.txt and f.txt
#    squash b7d542d Added file g.txt and h.txt
# No commands remaining.
# You are currently editing a commit during a rebase.
#
# Changes to be committed:
#       new file:   a.txt
#       new file:   b.txt
#

Here we need to put the final commit message,

# This is a combination of 2 commits.
# The first commit's message is:

Added file a.txt and b.txt

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Sep 21 12:09:18 2017 +0530
#
# interactive rebase in progress; onto 6d085c2
# Last commands done (2 commands done):
#    pick 06c23f5 Added file e.txt and f.txt
#    squash b7d542d Added file g.txt and h.txt
# No commands remaining.
# You are currently editing a commit during a rebase.
#
# Changes to be committed:
#       new file:   a.txt
#       new file:   b.txt
#

And save and exit from your editor.
Now we need to push these changes, we can do that by using the command:
git push origin +master

You can see that your commits has been merged into one:

P.S:
Difference between –force and +
Note that –force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).

For further reading: Git-Branching-Rebasing

Looping over file content in Python

While looping over files content line by line in Python we may face error like after the first iteration from the beginning to the end of the file we will not be able to iterate again over it 😦 .  This happens because in Python uses a seek() object to track the traversed lines.

For example: Consider we want to print the content of a file two times.

In below example, we are trying to print the content of a file named “test.txt”, which contains number 1 to 5 on separate lines.

Screenshot from 2017-08-30 17-44-59

Here, we are using for loop to iterate over it. As you can see that it is not printing anything on the second iteration.

To overcome this, we need to set the file.seek() pointer to the beginning of the file. We can do that by adding file.seek(0)

In the above program, we can do that by –

Screenshot from 2017-08-30 17-44-59

Hope you like it 🙂

Ubuntu: Installing specific version of a package

In this post, we will command to install specific version of a package and we will stop future upgrades of that package. So let’s start.

1. Let’s find out the version we want to install in our Ubuntu machine.
For this we can use policy option in apt command
apt policy <package name>
For example:
apt policy elixir
Screenshot from 2017-08-21 11-03-39.png

To install a particular version of a package we need to pass it’s version number as:
sudo apt install elixir=<version number>

We will now move ahead to install elixir v1.3.0-1.
sudo apt install elixir=1.3.0-1
Output:
Screenshot from 2017-08-21 11-12-32

As you can see in the above image it is saying that the package will be downgraded.
So till now we are able to download our specific package. Yipeee!!!

Now we will hold our packge so that it wont’t upgraded automatically.
We will use the following command for that:
sudo apt-mark hold <package name>
and to see the list of packages which are on hold,
apt-mark showhold
Output:
Screenshot from 2017-08-21 11-18-17

Now to remove a package from hold:
sudo apt-mark unhold <package name>

Yipee!! all set. Now our elixir package will not be upgraded on runnig system upgrades. 😀

Print the name of files of size zero or size non zero

I was trying to find all the files in a directory ($dir) and its subdirectories with size 0. To achieve that I have used the find command as:

$ find $dir -size 0 -type f
OR
$ find $dir -size 0 -type f -print

This will print all the files with size 0 🙂

If you want to delete those files we can do that by using the below command:


find $dir -size 0 -type f -delete

For example:
If I want to delete all the files in my home directory with size zero, I can do that by:

$ find ~/ -size 0 -type f -delete

Note: Don’t change the order of parameters in find.

To find the files of size non zero the command is:

 
$ find $dir -size +0 -type f
OR
$ find $dir -size +0 -type f -print