Linux.com

Home Linux Community Community Blogs

Community Blogs



Linux Shell Tip: Remove files with names that contains spaces, and special characters such as -, --

In Linux or Unix-like system you may come across file names with special characters such as:

  • -
  • --
  • ;
  • &
  • $
  • ?
  • *
  • White spaces, backslashes and more.

In this quick tip I am going to show you to delete or copy files with names that contain strange characters on Linux.

Sample file list

Here is a sample list of file names:

file-1

The problem and solution

Your default bash shell considers many of these special characters (also known as meta-characters) as commands. If you try to delete or move/copy such files you may end up with errors. In this example, I am trying to delete a file named '>file':

$ rm >file

Sample outputs:

rm: missing operand
Try `rm --help' for more information.

The rm command failed to delete the file due to strange character in filename.

Tip #1: Put filenames in quotes

The following command is required to copy or delete files with spaces in their name, for example:

$ cp "my resume.doc" /secure/location/
$ rm "my resume.doc"

The quotes also prevent the many special characters interpreted by your shell, for example:

$ rm -v ">file"
removed `>file'

The double quotes preserve the value of all characters enclosed, except for the dollar sign, the backticks and the backslash. You can also try single quotes as follows:

$ rm -v 'a long file   name  here'
$ cp 'my mp3 file.mp3' /backup/disk/

Tip #2: Try a backslash

You can always insert a backslash (\) before the special character in your filename:

$ cp "my\ resume.doc" /secure/location/
$ rm "\*file"

Tip #3: Try a ./ at the beginning of the filename

The syntax is as follows to delete a file called '-file':

$ rm -v ./-file
removed `./-file'

The ./ at the beginning of the filename forces rm not to interpret - as option to the rm command.

Tip #4: Try a -- at the beginning of the filename

A -- signals the end of options and disables further option processing by shell. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --. The syntax is:

$ rm -v -- -file
$ rm -v -- --file
$ rm -v -- "@#$%^&file"
$ rmdir -v -- "--dirnameHere"

Tip #5: Remove file by an inode number

The -i option to ls displays the index number (inode) of each file:

ls -li

Use find command as follows to delete the file if the file has inode number 4063242:

$ find . -inum 4063242 -delete

OR

$ find . -inum 4063242 -exec rm -i {} \;

Sample session:file-2

For more information and options about the find, rm, and bash command featured in this tip, type the following command at the Linux prompt, to read man pages:

$ man find
$ man rm
$ man bash
 

10 Linux Bash and KSH Shell Job Control Examples

Linux and Unix are multitasking operating systems i.e. a system that can run multiple tasks (process) during the same period of time. In this new blog series, I am going to list the Linux and Unix job control commands that you can use for multitasking with the Bash or Korn or POSIX shell.

Read more: 10 Linux Bash Shell Job Control Examples

 

cwrap 1.0.0 - Testing your full software stack on a single machine

FOSDEM/Brussels
 
Sunday, February 2nd 2014, Version 1.0 of cwrap, a project to test your full software stack, has been released at FOSDEM. cwrap is a set of tools to create a fully isolated network testing environment to
test client/server components on a single host. It provides synthetic account information, hostname resolution and privilege separation support. The heart of cwrap consists of three libraries you can
preload in any executable.
 
The cwrap project does not require virtualization and can be used to build environments on different operating systems. The project consists of a socket wrapper, NSS module wrapper (users, groups,
hosts), and a (s)uid wrapper with support for GNU/Linux, BSD and Solaris.
 
The origin of these wrappers is the Samba project, where the wrappers have already been in use for many years to successfully test the SMB and other protocols'implementations. Now it is possible to use them
outside of the Samba project. The wrappers have been enhanced with new features.
 
Learn more at http://cwrap.org/
 

How to update Ubuntu 12.04 LST

First thing you should do just after the installation is updating the Ubuntu. Ubuntu 12.04 is the LST version and would be supported until April 2017. If you have Ubuntu 12.04 freshly installed, very first thing after installation is to update all the repositories. This would save you from unmet dependency errors.

Read complete step by step Guide here

How to update Ubuntu 12.04 LST

 

Linux Game Sales Statistics From Multiple Developers

GamingOnLinux.com reached out to multiple game developers to get insights into how well their games are doing on Linux and these are the results.

http://www.gamingonlinux.com/articles/linux-game-sales-statistics-from-multiple-developers.2963
 

dstat Tool to Monitor Processor, Memory, Network Performance on Linux Server

Dstats is a versatile resource statistic tool. This tool combines the ability of iostat, vmstat, netstat, and ifstat. Dstat allow us to monitor the linux server resources in real-time. When you need to gather those information real-time, dstat will fit your need.

Read more... Comment (0)
 

Linux ip Command Examples

I am a new Linux system admin user. How do I use ip command line utility to display or configure networking, routing, and tunnels on Linux operating systems? How do I configures or displays network interface parameters for a network using TCP/IP on Linux operating systems? The ip command is used to assign an address to a network interface and/or configure network interface parameters on Linux operating systems. This command replaces old good and now deprecated ifconfig command on modern Linux distributions.

Linux ip Command Examples

 

My Nerd Life: Too Loud, Too Funny, Too Smart, Too Fat

Carla Schroder is a self-taught Linux and Windows sysadmin and the author of the Linux Cookbook and writer of thousands of Linux tutorials.

If there is only one message you take away from reading this, let it be this: Linux and FOSS do not need more glamorous elite uber-rockstar coders. We need more ordinary, dedicated individuals from all walks of life contributing however they can. Just plain ordinary people with whatever they have to offer.

I am a born nerd, born to take things apart and put them back together, and to combine unlike things in imaginative ways. I am one of those people you never want to go shopping with, because I have to stand in front of any item I might ever under any circumstances consider purchasing, and work through in my head the nine zillion ways in which I might use it ... and then move on to the next item and repeat the trance. Because why not? Is it not all about possibilities?

Read more... Comment (18)
 

Ubuntu 14.04 (Trusty Tahr) - Alpha 1 Released ! Time to Upgrade !

Even though the release of Ubuntu 14.04 Trusty Tahr is scheduled on April 17, 2014, you can enjoy using it today. Yes, the first alpha of Ubuntu 14.04 Trusty Tahr is available for the users. This article will help you to upgrade to Ubuntu 14.04 from Ubuntu 13.10 Saucy Salamander.

Before Upgrading:

1. Make sure that you are having the backup of all important data.
2. All the third party repositories should be disabled.
3. Owners of production systems are advised to wait till April 17 for the final release.

Read more at YourOwnLinux

 

My Nerd Story: From Record Store Clerk, to Tech Journalist and Community Manager

Rikki Endsley is a technology journalist and the USENIX Association’s community manager. In the past, she worked as the associate publisher of Linux Pro Magazine, ADMIN, and Ubuntu User, and as the managing editor of Sys Admin magazine. Find her online at rikkiendsley.com and @rikkiends on Twitter.

I've been a writer for as long as I can remember, which is why I was thrilled to receive an electric typewriter as a high school graduation gift in 1988. Asking for a computer was never something I considered. I don't remember ever being exposed to computers while I was growing up. After high school, I didn't even use my new electric typewriter for a while. Instead, I took a year off, continued working my record store job, and saw dozens of great bands.

My nerd story starts at a record store, with a cash register and a Schwann catalog.

Read more... Comment (3)
 

How to setup Virtualbox guest additions on Fedora 20

Fedora 20 (Heisenbug) is currently the latest release and if you want to try it on VirtualBox then install the guest additions for full functionality. Installation is simple and takes a few steps, but involves download and updates which would require some bandwidth. 1. Update Fedora The first thing to do is to upgrade all packages and make the system uptodate. $ sudo yum distro-sync 2. Install kernel headers and build tools VirtualBox guest additions are...
Read more... Comment (0)
 
Page 16 of 143

Upcoming Linux Foundation Courses

  1. LFD320 Linux Kernel Internals and Debugging
    10 Nov » 14 Nov - Virtual
    Details
  2. LFS426 Linux Performance Tuning
    10 Nov » 13 Nov - Virtual
    Details
  3. LFD312 Developing Applications For Linux
    17 Nov » 21 Nov - Virtual
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board