• List size of files and folders

    $ du parentFolder/* -sh
  • Free Space on Mounts and First Level Folders

    On a server I work with I have multiple physical volumes mounted under /mnt. Frequently I want to know the amount of free space available on each volume in addition to the first level contents that exist on the volume. The following command when run from /mnt or probably any place where your volumes are mounted will display the required information.

    for i in `ls`; do echo -ne "\E[1;31m$i "; df -h -P | grep `echo -n \`pwd\`; echo -n "/$i"` | awk '{print $4}'; tput sgr0; ls $i --color=never; echo ""; done;
      The steps that are taken are as such:
    1. obtain a list of the contents in your current working directory
    2. for each item on the list just obtained, get its absolute path. The absolute path is needed because this is what will be used to look up the space available on the df output
    3. now that we have the absolute path for a particular mounted volume, display the relevant column and row from the df output to get the proper available space
    4. echo the folder name and the available space left in bold red. The bold red is why the "\E[1;31m" craziness exists
    5. finally, list the first level contents of each drive without any special colouring

    I am not sure how this will translate to other situations, but, to make it a bit easier to understand my situation I may have several drives mounted like this: /mnt/sda1 /mnt/sdb3 /mnt/sdc2 and so on.

  • Digital Global Anarchy

    In the digital age location is irrelevant and there is little that can be done to enforce rules. There is little we can do to change this. We must learn to live with and embrace this state. Could this lead to a well functioning global anarchy?

  • Get Last Column of Each Row of Tab Delimited File

    I had a file that was of this form

    aaa bbb ccc hhh
    aaa hhh
    nnn vvv hhh
    vvv aaa eee aaa hhh
    Where each one of those spaces is a tab. I required the last column of each row. That is, I needed each field where "hhh" exists. And this was solved with the following code!

     $ cat tabDelimitedFile.txt | awk -F\\t '{print $NF}'
  • One Line, Rename Files in Directory

     $ ls | while read i; do mv "$i" "some prefix $i"; done;

    the read part of the while waits for input and takes the input and stores it in "i". The while loop gets it's input from ls. At each line of ls, the current line is sent to the read.

  • Delete Empty Folders

    This command will delete all the empty folders in your current directly and all its children.

     $ find . -depth -type d -empty -exec rmdir "{}" \;
    • The "." specifies that we want to search from our current directory.
    • "-depth" gives a recursive effect to this.
    • The "-type d" specifies that we only want to return directories.
    • "-empty" finds regular files and folders but the "-type d" part makes it so that we only find directories.
    • The items found after "-exec" are to be executed each time an item is found.
    • "rmdir" removes only empty directories. One could also write "rm -r" but that is less safe since it will also remove directories that are not empty.
    • The " "{}" " acts sort of like a variable that holds the name of the item that was found. The double quotes around it may not be needed, but, it didn't hurt the execution of the command when I ran it.
    • And, the "\;" finishes it all off.
  • Character Issues and MySQL

    A lot of times I work with databases with French characters or other characters and I finally found a solution that has helped in my particular case thus it may help in yours. It is to specify the the character encoding right on the command line with

    --default-character-set=utf8

    so, a command might look like

     $ mysql -u Tory -pMyPassword --default-character-set=utf8 < commandsToRun.sql 
  • Bash. Variables. Sed. Replace. Slashes

    Here is an example how to replace text containing slashes with new text that also contains slashes. It will ultimately convert "A cool website is http://www.yahoo.com" to "A cool website is http://www.google.com"

    #!/bin/bash
    A="http://www.google.com";
    B="http://www.yahoo.com";
    C="A cool website is $B";
    echo "A = $A";
    echo "B = $B";
    echo "C = $C";
    A=`echo "$A" | sed 's/\//\\\\\//g'`
    B=`echo "$B" | sed 's/\//\\\\\//g'`
    echo "----------After Running Fixing----------";
    echo "A = $A";
    echo "B = $B";
    echo "C = $C";
    echo "-----------After running sed------------";
    echo "$C" | sed "s/$B/$A/g"
  • Reading Files by Line w/ Spaces

    #!/bin/bash
    SAVEIFS=$IFS;
    IFS="\n";
    while read LINE ; do
         echo "${LINE}";
    done < myFileWithSpaces.txt;
    IFS=$SAVEIFS;
  • Finding Folders

    This command will find all folders in your current directory and deeper where the folder name has the word "tea", and then later followed by "cup" with anything in between. This will also ignore case.

    $ find ./ -iname *tea*cup* -type d
      This will match folder names such as
    • tea cup
    • TeA and Cup
    • wickedTeaCUP
    • super.tea.in.my.cup.right.now
  • Count Number of Lines Given by any Command

    Here is how you count the number of files outputted from any command using the ls command as an example

     $ ls -A | wc -l

    This is a bit curious actually because ls -A doesn't necessarily print each file on each line but the line count is still equal to the number of files. I presume that this is because bash is deciding not create newlines where new line characters exist. There is a simple way to test to see if this is true. If we run

     $ ls -A > test.txt 

    The output of ls -A will be sent into a file named test.txt instead of the wc program. After having done that we can run

     $ cat -E test.txt

    which will represent each newline character with a $. And, as I have just found out, and as you'll be able to see, there is a $ after each file. Thus, when you run ls -A the newline characters are there, the shell just decides to not necessarily print each file to a newline

  • Apache Alias / Directory + Sharing Files Locally

    Here is how I shared a directory on a web sever so that people could simply download some files. I made a change to the /etc/apache2/sites-available/default file, which is actually the exact same file as the /etc/apache2/sites-enabled/default file. The file in the sites-enabled folder is just a symbolic link to the sites-available file. It is done this way so that one can simply run the a2ensite to enable the site or a2dissite to disable the site or course followed by an apache restart with the command "service apache2 restart." a2ensite and a2dissite can be though of as apache2enablesite and apache2disablesite. so, as I was saying. In the /etc/apache2/sites-available/default file I added the following within the section:

        Alias /publicFiles/ "/home/tory/publicFiles"
        <Directory "/home/tory/publicFiles">
               Options Indexes MultiViews FollowSymLinks
                   AllowOverride None
                   Order deny,allow
                   Deny from all
                   Allow from 192.168.1.0/24
        </Directory>

    This will only allow people on my local network to access the file via the address http://myServer/publicFiles/. When people go there they will be greeted with a plain file listing. Within the folder /home/tory/publicFiles I did things like:

     $ ln -s /mnt/coolDrive/coolFileToShare.jpg heySisterCheckThisOut.jpg

    and of course all files need the proper permissions.... and no, that does not mean it is time to whip out the chmod 777 * -R ... lol

  • Create Several Files or Folders - Shell Expansion

    Today I learned a new method of shell expansion that I thought was pretty cool. Here is an example of how to create files by the names of: file1, file2, file3, file4 and file5

     $ touch file{1,2,3,4,5} 

    and do create 5 folders in the same way you could

     $ mkdir folder{1,2,3,4,5} 

    essentially the touch or mkdir command runs 5 times, one for each of the coma separated values within the squigly brackets. So with that said by no means is this limited to making files and folders. You could for example move several files into a single folder

     $ mv file{1,2,3,4,5} someFolder 
     $ mv {julia,matt,kattie}\'s\ work peoples\'\ work
  • Fixing Toilet Paper Plies that Don't Align

    Do you experience toilet paper where the tear lines don't line up? Well, this post is for you.

    As you can see here, the posterior and anterior plies do not align

    Toilet Paper Plies Out of Sync

    Further evidence of this malalignment is shown here

    Toilet Paper Plies Out of Sync

    Fortunately the fix is easy, hold on to exterior ply

    Beginning to Unwind Outer Ply

    Begin to unwrap it from the roll

    Unwinding the Outer Ply

    Follow through, and complete the de-wrapping process, leaving the roll that requires one obvious last step until completion of the toilet paper alignment procedure

    Tear Marks Aligned with Excess Plies

    Remove the excess plies and put them to some sort of good use

    A Toilet Paper Roll No Longer In Wonky Land

  • New Line + sqlite3 + Insert + Bash

    In a bash script was having no luck with

    MYTEXT="my text \n to be insert with two \n new lines";
    sqlite3 mydb.db "INSERT INTO myTable VALUES('value1', 'value2', '$MYTEXT')";

    because it was literally putting the \n into the database when it should have literally been a new line. This was solved by

    MYTEXT=`echo -e "my text \n to be insert with two \n new lines"`;
    sqlite3 mydb.db "INSERT INTO myTable VALUES('value1', 'value2', '$MYTEXT')";

    the -e on the echo allows for the new line characters to be interpreted as new lines as opposed to just a backslash and an n.

Page: 5 of 6