-
List size of files and folders
-
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.
- The steps that are taken are as such:
- obtain a list of the contents in your current working directory
- 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
- 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
- echo the folder name and the available space left in bold red. The bold red is why the "\E[1;31m" craziness exists
- 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! -
One Line, Rename Files in Directory
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.
- 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
so, a command might look like
-
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"
-
Reading Files by Line w/ Spaces
-
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.
-
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
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
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
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: 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:
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
and do create 5 folders in the same way you could
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
-
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
Further evidence of this malalignment is shown here
Fortunately the fix is easy, hold on to exterior ply
Begin to unwrap it from the roll
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
Remove the excess plies and put them to some sort of good use
-
New Line + sqlite3 + Insert + Bash
In a bash script was having no luck with
because it was literally putting the \n into the database when it should have literally been a new line. This was solved by
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.