Tech Category
I wanted to replace all hyphens in all filenames starting with 2003- and ending with .php. Here's how I did it using sed (no guarantees, no warranties, use at your own risk!):
- Backup the files!
- Change to the directory you want to make the changes in.
- Then, to make sure the right filenames will be changed, list the filenames using the following sed command (modify "2003-*.php" and the sed search and replace strings for your situation):
for filename in 2003-*.php; do echo $filename | sed \e 's/\-/\_/g'; done - When you're sure the above sed command is listing the right filenames, change it as follows (modify "2003-*.php" and the sed search and replace strings for your situation):
for filename in 2003-*.php; do newname=`echo $filename | sed \e 's/\-/\_/g'`; mv $filename $newname; done
Note the use of the backtick character (`) (also known as backquote) which, on my keyboard, is located to the left of the "1" key. The backtick character is used in addition to the single quote character (').
[Update January 4, 2009] I totally forgot that I could have used the "rename" command instead of sed! There is control over which underscore in a filename is changed. More about renaming files using "rename".
I bought an external hard drive in December 2007 so I could backup my PC. Of course, the box containing the drive stayed unopened on the floor of my home office until this past Saturday - yes, 7 months of having but not using a backup system!
I bought a Western Digital WDG1T7500N My Book Pro Edition 750 GB from Amazon for $283 - the price is now $329.99. Using the Retrospect backup software that came with the drive, I setup an automatic daily backup running at 12:00am each evening which I may change because the backup doesn't run when the PC is in standby mode.
I tested a restore and it worked great!
I committed a technical support faux pas at work yesterday. I moved my phone from one side of my desk to the other and then it no longer worked. No sound. I checked the connector to the wall - ok. No sound. I pulled the connector out of the wall port and re-connected it. No sound. I checked the volume control - ok. No sound. I turned over the phone to make sure the cable was secure - ok. I placed a helpdesk request: "my phone is DEAD".
The phone guy from IT visited today mid-morning…
I {heart} UNIX! In addition to the 'find' commend, I just found another very useful command that is now my second favorite!
rename
The rename command renames files according to a pattern, such as:
I needed to rename almost 800 files from a format like "2007_XXXXXXXXXXXXXX.php" to use a hyphen (dash) instead of the underscore after the year - such as "2007-XXXXXXXXXXXXXX.php". I did this with the following for each year (there's probably a way to rename all years at once, but I didn't have a backup and didn't want to screw it up):
rename 2007_ 2007- 2007*.php
which means "change the string 2007_ to 2007- in all filenames resulting from 2007*.php".
Worked like a charm!!!
I am not responsible for any problems you may encounter. Backup all files first.
I started working for my current employer as their Web Designer almost 6 years ago. I worked with one of the System Administrators to setup a new web server environment and processes. She built two SuSE UNIX servers (one for development, the other for "live" published websites), installed the Zope web content management system, and used Apache to handle the website. She kept all access to root and managed the hardware and software on the servers - even today, I don't know where they live on campus!
When she left about 2 years ago, one of the other System Administrator's inherited management of "my" web servers. Soon, however, he agreed to give me root access so he wouldn't have to change file/directory permissions for user directories, add Rewrite commands to the httpd.conf file, modifying other config files (ht://dig, Webalizer, etc.), manage large files when the Zope database needs to be packed, and do other administrative tasks on the web servers that took him away from bigger projects. That was okay with me! I'm comfortable at the UNIX command line and am perfectly capable of doing those tasks.
When the servers were setup, directory browsing was turned off on the server for better overall security. A year or so later, one user wanted to be able to list the files in a single website directory, but the System Administrator wasn't able to set httpd.conf so that it worked. Then, about 2 years ago, another user (a more demanding one) wanted to do the same thing with a bunch of his Camtasia files. The one remaining System Administrator at that time was a Microsoft Windows only guy so nothing could be done. I learned a bit about httpd configuration commands but was unsuccessful in getting "Options Indexes" to work. So, I wrote a PHP script to be placed in a directory as index.php. When that file is accessed in a browser, it displays the foldernames and filenames contained in that directory and its subdirectories. It worked great!
But it's always bugged me that directory browsing wouldn't work on the servers! Well, today I spent most of my work day Googling away for anything that could help (such as (no quotes) "disable directory browsing", "enable directory browsing", "enable directory browsing for one directory", "suse apache directory browsing") and making modifications to the httpd.conf file on the development server. I must have restarted Apache 50 times or more!
I learned what the httpd configuration commands do (important!). I discovered Include statements in the httpd.conf that included *.conf files for SuSE, and thought maybe I had finally found where the problem might be. No luck.
About 30 minutes before the end of the day, I Googled "directory browsing not working" (no quotes) and found this thread on a forum that pointed me to, ta da, the solution!
From Ddr/Dale's response post:
-------------------------
IndexIgnore *(will prevent listing of all files, the "*" is a wildcard)
-------------------------
IndexIgnore *.gif *.jpg(will prevent listing of any files with .gif or .jpg extensions, but will allow other listings)
-------------------------
Could it be? Was there an "IndexIgnore *" command in the httpd.conf file?
Yes. Yes, indeed. It was there, bold and bright as day, jumping from the screen saying "I'm here, I'm here!" (now that I knew what it meant)!
So, I removed it. And all was fine with the world again. Well, maybe just my little corner of the world.
Summary:
- To disable directory browsing, use "Options -Indexes" or remove "Indexes" from an "Options" command (i.e., "Options Indexes FollowSymLinks" becomes "Options FollowSymLinks").
- To enable directory browsing, use "Options +Indexes" or add "Indexes" to an "Options" command (i.e., "Options FollowSymLinks" becomes "Options Indexes FollowSymLinks"). Make sure "AllowOverride Options" is included in the httpd.conf file in a <Directory> section for the directory where the DocumentRoot is.
- Do not use "IndexIgnore *" to disable directory browsing. It is used to disable display of certain folders/files - not all folders/files - while directory browsing.
Now I feel I must address why I am a former UNIX System Administrator with 10+ years experience and have not worked with Apache configuration!
Way back before Windows 3.1, my employer used UNIX servers with terminals (we referred to them as dumb terminals because they had no processing capabilities - they just displayed what was delivered to them from the UNIX main frame computer).
I was the System Administrator for the UNIX systems. Again, that was before Windows 3.1 - and before the World Wide Web became widely used with the help of GUI web browsers.
Anyway… I was the System Administrator for those 8 or 9 systems. As such, I created UNIX user accounts, configured software (including UNIX-based WordPerfect!), wrote shell scripts, troubleshooted problems, created/managed Informix databases, wrote programs to interface with the databases, considered the vi editor and the "find" command my best friends. And more. Even after the WWW became popular, my employer didn't have a website until, I think, 1997 or 1998, but I had already left that employer in May 1996. But that's not the point here…
The point is: Although I was a UNIX System Administrator until 1996, I have never been the administrator for a UNIX-based web server (such as Apache)! Sure, I've played around with my own web hosting accounts on UNIX servers using the Apache web server since 1998 (maybe earlier?), but I haven't gotten into the nitty gritty of how Apache works.
Until today!
I wanted to change "/artscrafts/" to "/blog/" in all popup image files (these are not stored in the MT database so search-and-replace inside the Movable Type 4.01 administrative interface doesn't work for those files). The file for each popup image is stored in the same directory as the popup image.
The following worked for me, but there is no guarantee that it will work for you. I am not responsible for any problems you may encounter. Backup all files first.
To change a string of characters to a different string of characters in multiple files:
- At a Unix prompt, cd to directory where the files to be modified are located
- Type on one line:
for f in *php; do sed 's/string1/string2/g' $f > $f.new && mv $f.new $f; done
Line 2 means: For every file ending with php, replace string1 with string2 and save the result into a new file, then move the new file over the original file.
For each slash in the string, I needed to use \ in front of it, as follows:
for f in *php; do sed 's/\/artscrafts\//\/blog\//g' $f > $f.new && mv $f.new $f; done
Multiple lines can also be used at a Unix prompt as follows:
for f in *php
do
sed 's/string1/string2/g' $f > $f.new &&
mv $f.new $f
done
Based on information in a thread at dBforums. Note: That code uses the Unix find command to locate files which could be in subdirectories. Back up all files and learn about the find command before using.
for fname in $(find . -name "*" -print)
do
sed '/word/s/word/new_word/g' ${fname} > ${fname}.new &&
mv ${fname}.new ${fname}
done
After all the re-publishing and researching I did yesterday, I realized that the filenames on my old blog site used underscores - not dashes - and, because of that, some links were broken on this new blog! Ack! (I knew I wanted to use dashes so I thought I had already been using dashes on the old blog!) I was by then too tired to try to fix it.
Today, however, I was ready to tackle this new problem! A Google search for "htaccess underscores dashes" revealed just what I needed! Using .htaccess Voodoo For Underscore and Dash Woes provides the solution with the exact lines to copy and paste into the .htaccess file. Changes I made:
- There were 2 extra spaces that caused 500 errors until I removed them from the last and next-to-last lines just before \.html$
- Changed "RewriteBase /" to the subdirectory for this blog, i.e., "RewriteBase /blog/"
- Since I use .php extensions, I changed .html to .php
- Added lines for $7, $8, $9
,$10, and $11because full titles use more than 6 words in some cases
Works like a charm!
[Update October 28, 2007] It seems that the wildcards go to only $9. So I added this line (it's one line, not three!) above the $9 line - it catches the first nine wildcard variables then the lines below this one catch the rest.
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)
_([^_]*)_([^_]*)_(.*)\.php$
http://www.tamemymind.com/$1-$2-$3-$4-$5-$6-$7-$8-$9.php









Recent Comments