Listing absolute paths with `ls`

Normally when you list the files in your directory/sub-directory with the ls command you would get the paths to those files with respect to the current directory.

But what if you wanted to save this list and you wanted the absolute paths rather than the relative paths. Well it doesn’t strike you first but then when you know it, its like, “Oh its that obvious!”

So you do

ls -d $PWD/what_ever_you_are_trying_to_list

The $PWD gets filled with the path to the current directory.

Example:
ls work/*.md

might have yielded

work/this.md
work/that.md
work/something.md
work/someotherthing.md

But,
ls -d $PWD/work/*.md

would yield something like

/home/username/work/this.md
/home/username/work/that.md
/home/username/work/something.md
/home/username/work/someotherthing.md

Advertisements

Execute Script Without Exiting Vim

Two ways,

Say you are writing a python or bash script, you can say

:!scriptname

This assumes you have your shebang line at the beginning of the file. This would work well.

Else do this for more options,

:set makeprg=python\ %
:make

Good Uses for Old Phones

We live in a day and age where mobile phones are as common as shirts and pants. There is a good possibility that a lot of good phones that are lying around in your house which can be put to good use. Here I list a few ways in no particular order. Some of these functions can only be performed by a smart phone, some by ordinary phones as well.

  1. Download Manager – If you have a slow wifi connection at home, your downloads might take a really long time to complete. If you have an old android phone lying around, you can install a torrent downloader on that and any sort of download manager that you need and start downloading. You might have some old memory card lying around as well. If that is the case, then well and good. You are all set for your download journeys. This is less power consuming as opposed to leaving your desktop on all the time as well.
  2. Timer – Timers come of good use in a lot of places. For example, in the kitchen; when you want to time your game time, tv time and so on. There is one good technique called the pomodoro technique that works based on the principle of concentrating for 20 minutes and then relaxing for five. Repeating this set three times and then taking a long break for 15 mins at the end of those three cycles. It can help you be more productive. I have tried it and it works. Use your old phones for that, you will save yourselves from the distractions of all the notifications from your current phone.
  3. Music Player – Your current phone may have a lot of space, but I am sure you would want more. Why not push all the music to your old phone and just have the hits on your phone. This works well if you don’t own an iPod and you probably don’t need to. An old phone works really well for this.
  4. GPS Device – How many times have you been frustrated with maps and GPS consuming your battery like crazy. Why not use an old phone for that. You don’t have to worry about your phone going out of battery at the end of the journey. Sure you can use your car charger but isn’t it less fussy to just forget about having to charge your phone all the time.
  5. E-Reader – If your old phone has a good screen you can use it as an e-Reader and save battery on your current phone.
  6. Backup Phone – When you have your old phone lying around for any use mentioned above. You will have it charged. This will come in handy when your current phone dies and you don’t have a power bank with you.
  7. Fitness Tracker – Install a fitness tracker that requires just your accelerometer and your GPS. You will again not have to worry about your battery draining in such activities.
  8. Donate It – Last but not least. Rather than just keeping in a shelf and letting it die, give it to someone who really needs it. You will have earned their love.

pushd and popd – File Navigation Gems

pushd and popd are two commands that are absolutely worth their learning time if you are in the business of using the command line on a regular basis on linux. One of the biggest hindrances of using a command line for file manipulation and such would be navigation between folders. Sometimes its might be a pain to navigate between two folders, even using TABs for auto completion. This is where pushd and popd comes in to the picture.

What this does, in short, is, create a stack containing the paths that you have traversed using pushd. You can revisit these paths using popd. Remember that this is a stack in all aspects.

A short intro on how a stack works, if you haven’t come across it already. Skip this paragraph if you know what a stack is. Stack is a data structure that is like a stack of books or a stack of biscuits or best, pringles. You can stack one item on top of each other, but you can only take the item on top. Logical  right?. You stack an item using push. As in, pushing a potato chip into a pringles case. And you take one out using pop. Of course, you can only take the one on the top, a position called the ‘top of stack’.

Example:

Suppose you are in your home folder (~). You move to another directory by typing,

Command: pushd ~/Documents/work-docs

This will give you an output with two directory listings. The first one is the directory that you have navigated ‘to’. The second one is the directory that you just navigated ‘from’.

Output: ~/Documents/work-docs ~

You can navigate to another folder with something like say,

Command: pushd ~/Downloads

Now you will have the following output

Output: ~/Downloads ~/Documents/work-docs ~

What this shows is a history of your past navigations, or a ‘stack’ of your previous directories. The top of stack being the left most. Now to go back one step to ~/Documents/work-docs/, all you have to do is type,

Command: popd

and it will give you the following output

Output: ~/Documents/work-docs ~

That would be a quick intro of pushd and popd. Have a good time navigating through your directories.

Compress, Split and Join Tar files – Linux

Creating a tar file and splitting it for storing it in cds, dvds or any other storage with small capacity.

Create a tarball and compress it using bz2 or gz compression algorithms by using one of the following commands.

tar -cvjf tarofsomefolder.tar.bz2 /home/someone/Documents/*
tar -cvzf tarfileofsomeiso.tar.gz some.iso

To split you can use the split command
split -b 4500M tarofsomefolder.tar.bz2 "tarofsomefolder.tar.bz2.part"

You can use both of them together in a single command by piping the outputs of the tar command to the split command,

tar -cvzf downloads.tar.bz2 ~/Downloads/* | split -b 4500M - "downloads-part"

To extract these split tar files together, you have to cat those individual tar files either by listing them one after the other or by using a regular expression that covers all of it and then piping the output to a tar extract command. Something like,

cat downloads.tar.bz2.part* | tar -xvzf -

Mount a disk – Linux – cmd

To mount a disk (usually a file system that you usually go and click on using nautilus/caja (file browsers)) from the command line, use the following,

udisksctl mount -b /dev/disk/by-label/

Listing the content of /dev/disk/by-label/ will give you the names of the mountable disks/drives. Usually flash drives are mounted automatically so they do not have to be mounted. Unless you unmount it and want to mount it again. Use man udisksctl for more information. Also, I think it is udisks in some distributions.

Wget – Download from a list

If everyone started to read the manual then bloggers like me would…. eh no… I would still blog even if no one reads this.

This is pretty simple but it is really nice and may inspire you to investigate more. If you have a list of links to download all you have to do is put them all in a text file. One in each line. Then fire up wget from the terminal like this

wget -i path/to/textfile

 

This will download all of those files one by one and save it in the present working directory. Really good if you already have a list of links to download. Or if you have a set of links to download from on  the same page you can use some download plug in like downthemall on firefox.

You can use wget to do a lot lot more. Read the manual. You can use wget on bash scripts as well, since it is a command line tool. Imagine the possibilities. You could write a bash script with regex to get all files with certain patterns from a particular location. I once downloaded a full website containing more than a 1000 html pages with wget, and it was really good. But that was simple. Not much scripting involved though. You just have to look for the proper parameters to use with wget.