Linux.com

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)
 

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)
 

18 things I did after installing Fedora 20, the Xfce spin

Fedora with Xfce Dis-preference for Gnome 3 and un-necessity of KDE is the reason why I mostly choose the Xfce desktop when working on or trying out newer or unknown distros. And when working at length, I always choose to tune the desktop to my whims. Xfce is perfect when productivity is high on priority but not at the cost of functionality or looks either. And this post compiles a list of better-ments I...
Read more... Comment (0)
 

Steps to Install Wine 1.7.11 on CentOS, RHEL and Fedora

Wine is an Open Source implementation of the Windows API and will always be free software. Approximately half of source code is written by its volunteers, and remaining effort sponsored by commercial interests, especially CodeWeavers.

 

Read complete article at Install Wine 1.7.11 on CentOS, RHEL and Fedora. This article will help you to install Wine 1.7.11 using source code by compiling it.

 

Unleashing the Best Open Source Social Networking Software

The open source community plays an important role in the social networking space. It helps individuals create their own social network easily. With the software featured in this article, users can take more control of their site, and help establish and maintain a connection between users of the site.

New social networking platforms keep appearing from every corner. Unlike newcomers, all of the mature software packages featured here are professional, have a good feature set, and are easy to install and configure.

<A HREF="http://www.linuxlinks.com/article/2014011804122865/SocialNetworking.html">Read more</A>

 

Using preload to Speed up Linux

Preload is an ‘Adaptive Read-ahead Damon’, which is the equivalent to Windows Vista’s Superfetch. Effectively what it does is speeds up application load time by monitoring the software that is loaded and used day to day, the software used most often, and cache them in memory. If you have a lot of memory, you will notice things will improve – for example, my work machine has 20gb of RAM, 7gb of it is used by caches and everything runs nice and smooth. If your computer needs the memory, space is made, so you will not lose out if you have the average 4-8gb. The difference is certainly measurable – from 20% to 60% improvement in startup times.

Installation is simple – its supported on most platforms, and can be installed through your repository, for example, Debian and Ubuntu people can use the simple apt-get install preload to install the software. For the more adventurous, you can grab the source code fromhttp://sourceforge.net/projects/preload/.

Configuration wise, the defaults will work happily enough, but for the people who like to tinker, configuration can be found in /etc/preload.conf.

Information on configuring preload can be found within the documentation the author has written. This can be found at http://techthrob.com/2009/03/02/drastically-speed-up-your-linux-system-with-preload/preload_files/preload.pdf.

 
Page 10 of 131

Upcoming Linux Foundation Courses

  1. LFD331 Developing Linux Device Drivers
    25 Aug » 29 Aug - Virtual
    Details
  2. LFD411 Embedded Linux Development
    25 Aug » 29 Aug - Santa Clara, CA
    Details
  3. LFS422 High Availability Linux Architecture
    08 Sep » 11 Sep - Raleigh, NC
    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