Quick Linux Tips: File Naming - gHacks Tech News

Quick Linux Tips: File Naming

If you are coming from the world of Windows you are pretty much used to naming a file whatever. You could have a file named "This Is My File And This File Is Called Meh.doc" In the world of Windows you wouldn't have any problem with that file. In the world of Linux you might run across some issues.

Of course you could open up Nautilus, Dolphin, Thunar or any other file manager tool, double click on that .doc file, and start working on it in your word processor of choice. But what if you wanted (or needed) to move that file via command line? What would you do?

If you attempted to issue the command:

mv ~/Documents/This Is My File And This File Is Called Meh.doc ~/

you would see:

mv: cannot stat `This': No such file or directory
mv: cannot stat `Is': No such file or directory
mv: cannot stat `My': No such file or directory
mv: cannot stat `File': No such file or directory
mv: cannot stat `And': No such file or directory
mv: cannot stat `This': No such file or directory
mv: cannot stat `File': No such file or directory
mv: cannot stat `Is': No such file or directory
mv: cannot stat `Called': No such file or directory
mv: cannot stat `Meh.doc': No such file or directory

penguin_smallNow if you started typing the command:

mv ~/Documents/This

and you hit the Tab key bash would complete the file name, only it would look like:

This\ Is\ My\ File\ And\ This\ File\ Is\ Called\ Meh.doc

at which point you could finish typing out the command with sucessful results.

To avoid this all together, don't use spaces in file names when using Linux. If you need to separate words use either a "-" or an "_" to separate words like so:

This_Is_My_File_And_This_File_Is_Called_Meh.doc

Of course the above file name is rather cumbersome. But you get the point.

No spaces in file names under the Linux operating system.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Dotan Cohen said on April 13, 2009 at 2:22 am
      Reply

      Or, you know, just don’t use the command line.

      I have installed Ubuntu on over twenty friends’ and relatives’ computers, and _none_ of them, not one, touches the command line. I do, because I like it, and I even have spaces in my filenames.

      In other words, don’t let these Scary Linux “tips” scare you away. Spaces in filenames are just fine in Linux. I’ve been using them for years. And no, you don’t have to touch the scary command line in Linux any more than you needed it in Windows (yes, it was there).

    2. jack said on April 13, 2009 at 3:57 am
      Reply

      Dotan: No, you don’t have to use the command line. In fact, I would challenge anyone to see how long they can use Linux every day without touching the command line. You’d be surprised how long you can go. I have been using it for so long, however, the command line is second nature to me.

      So yeah – you can go without it. But in many instances it’s more efficient than ANY front end tool. To that end I want to make sure people know how to use it wisely – that includes the naming of files.

    3. FallenPX said on April 13, 2009 at 5:53 am
      Reply

      You can overcome the spaces simply by using (“). For example:

      mv “~/Documents/The file to be renamed.txt” “~/Documents/The renamed file.txt”

    4. jack said on April 13, 2009 at 2:38 pm
      Reply

      Yes you can over come the spaces in a number of ways. My point was to avoid them so you don’t have to over come them. It is my believe that the Windows way engenders bad habits with users.

    5. Rick said on April 13, 2009 at 6:49 pm
      Reply

      It’s ludicrous in this day and age to avoid using spaces in filenames. Filenames should clearly describe their contents, be easy to type, and be easy to read. Just use quotation marks like FallenPX says, when you’re not using tab completion.

      You can even use quotation marks with tab completion if you want it to look pretty.

      mv “~/Documents/This
      and hit Tab and it will complete the commandline to look like this:
      mv “~/Documents/This Is My File And This File Is Called Meh.doc”
      unless there are multiple matches, at which point it continues normal tab completion behavior but uses spaces and quotation marks.

    6. The Mighty Buzzard said on April 13, 2009 at 11:10 pm
      Reply

      @jack: Sure I could go without using the command line if I really tried. The question is, why would I want to do that on purpose?

    7. CutTheRedWire said on April 14, 2009 at 2:49 am
      Reply

      Or, do what I do: avoid spaces.

      In scripts or programs, names with spaces will drive you nuts. In files for settings, heh, ya… not a good idea (hello skinner’s nightmare). In a document (e.g. contact.html) I want to access a lot, no spaces helps. I never know where I’m going to add it, and I open it like a program. I do this with some wallpapers too, which allows for imaginative access. Lastly, any files I’m going to upload I keep the spaces out of. I%20hate%20spaces%20in%20urls!

      Don’t get me wrong, I’m big on spaces too. For massive amounts of files, it’s easy to read & hard to avoid.

      PS: Speaking of file naming ideas, tip them off about Gregorian dating systems! 8) …. Resume-Red-2009-04-13.doc anyone? ^.~

    8. Rick said on April 14, 2009 at 5:27 pm
      Reply

      I never knew that style was called “Gregorian”. That’s the style I use for dates in filenames, so when sorted by name they sort by date.

    9. anon said on August 25, 2009 at 9:28 pm
      Reply

      Just bear in mind that command line doesn’t like spaces without a backslash, also, if you like capital letters, have a system so you remember, gets irritating ‘downloads’ vs ‘Downloads’ so folders i do capitals and files i use lower case, keeps things easier to remember.

    10. Archangel said on October 22, 2009 at 4:47 pm
      Reply

      I don’t like spaces in filenames, I did not when I used Windows and I do not now that I moved to Linux. But I think this issue has nothing to do with Windows vs Linux.
      If you are a power user (of both OSs), you use the command line, the keyboard shortcuts, etc. and spaces in filenames are an annoyance.
      If you are a naive user, and use your mouse 90% of the time, then this issue is irrelevant.
      Indeed, Linux has a more powerful shell, so you probably end up using it more and more, but there is nothing that forces you to do so.

    Leave a Reply