Rename a file that starts with a hyphen/dash

While working on your *nix based system you may run into this issue from time to time. The file may have a hyphen (-) at the start of the filename because of user error. Or it may have been added by a malicious bit of software. However the hyphen got there it can be a pain to deal with. In this post we will answer how to rename a file that starts with a hyphen.

The first time I encountered a filename like this was while searching the files of a compromised website. The offending malware had created several different new files on the site. And one had been created with a starting hyphen in the filename. This causes issues when you run ‘rm’ to remove the file.

rm -filename.php

The hyphen typically tells a command line utility to anticipate an execution option. So in this case the ‘rm’ command attempts to interpret -filename.php as an option. This would just cause ‘rm’ to return the following on a linux based machine.

rm: invalid option -- 'l'
Try `rm --help' for more information.

Both -f and -i are options for the command so when it gets to ‘l’ it assumes it is just an invalid option. So this becomes an issue when you try to remove or otherwise edit/manipulate a file with a hyphen in the name.

Dealing with the hyphenated filename

Fortunately there is a relatively easy method to deal with a file that starts with a hyphen/dash. Adding a double dash (–) before the filename will fix it.

 rm -- -filename.php

This behavior should be universal for GNU/Linux commands. The double dash/double hyphen tells the command that no more command line options will be given. As a result your favorite command will ignore any further dashes and allow you to mv/cp/rm the file.

So now you know how to rename a file that starts with a hyphen on the command line. Hopefully that makes your life a little better.

I originally learned of this technique from this helpful post on superuser.com.

Want to learn how to recursively delete specific files, check out this post to find out how.

Recursive Find and Replace on the Command Line

The Problem

Recently I moved a website that used a ton of legacy php code from the clients production server to a development location. After the move was complete I found that the previous developers had been extremely sloppy. Rather than having a single location/file for DB credentials, they had it in 4 places.

After I figured out where all the locations for DB credentials were I started getting Open_Basedir errors. The original developer had hard coded the web root location hundreds of times in hundreds of files. For just a moment I felt just a bit overwhelmed, then I remembered that I have the terminal to solve problems like this.

The Solution: Recursive Find and Replace on the Command Line

After a bit of research I came up with the following command to recursively search through the entire codebase. When an instance the old web root is detected it replaces it with the correct one.

grep -rl [search for string] . | xargs sed -i s@[search for string]@[replace with string]@g

Or another example with actual search/replace strings

grep -rl /var/www/vhosts/example.com/httpdocs . | xargs sed -i s@/var/www/vhosts/example.com/httpdocs@/var/www/vhosts/newdomain.com/subdomain/dev@g

That command breaks down in the following manner.

“grep -rl” searches recursively for the string you specified “/var/www/vhosts/example.com/httpdocs” starting in the current directory “.”, the “-r” option specifies the recursive search, and “-l” specifies that the system should return only the filenames that contain the string.

those results are then piped “| ” into “sed”, the “-i” option specifies that it should make the changes in place. Then the find replace sequence in this case “s@[search for string]@[replace with string]@g”. The “@” signs could be almost any other value, typically they are a “/” but in this case the strings to find and replace both had “/” in each one so it wouldn’t work as the bordering character. So replacing the “/” with “@” helps SED keep on track. It could easily have been a “#” or “$”, just use what you need to depending on your string.

And with that I was home free no more Open_Basedir issues. Thanks to the command line recursive find and replace all those entries didn’t have to be done by hand.

Thanks Linux Shell

Here are some of the resources that I used when researching this topic:

Find and Replace string in all files recursive using grep and sed

Now you’ve learned to perform a command line recursive find and replace, why not read this post. Learn how to recursively delete specific files using the command line.