LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-23-2010, 02:37 AM   #1
chengbao1
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
Selecting files with ^M in the filename


I have a directory where some files contain the ^M special character in their filenames. (not sure how they got there, possibly related to editing them on Windows)

ls shows a question-mark in those filenames.
tab-completion shows a ^M instead of a question mark.

I'd like to get a list of all those files and delete them? How can I do that?

I've tried this:
ls *?.txt
ls *^M.txt
but both create errors

Thanks for your help!
 
Old 10-23-2010, 08:31 PM   #2
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by chengbao1 View Post
I have a directory where some files contain the ^M special character in their filenames. (not sure how they got there, possibly related to editing them on Windows)

ls shows a question-mark in those filenames.
tab-completion shows a ^M instead of a question mark.

I'd like to get a list of all those files and delete them? How can I do that?

I've tried this:
ls *?.txt
ls *^M.txt
but both create errors

Thanks for your help!
escape the "^"

ls *\^M.txt
 
Old 10-24-2010, 01:26 AM   #3
chengbao1
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the response, but that doesn't work. I get this error:

ls *\^M.txt
ls: cannot access *^M.txt: No such file or directory

I'm on Ubuntu 8, in case that matters.

I directly typed in "^M". Hitting CTRL+M just gives a new-line.

Any other ideas?
 
Old 10-24-2010, 02:40 AM   #4
chengbao1
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
I just solved the problem by writing a PHP script that checks for "\r" in the filename. Here's the source in case anyone wants it.


<?php

// returns true/false if $file contains ^M
function file_contains_special_char($file)
{
if (strpos($file, "\r"))
{
return true;
}
else
{
return false;
}
}

$directory = "/write/a/directory/here/";

// create a handler for the directory
$handler = opendir($directory);

// open directory and walk through the filenames
while ($file = readdir($handler)) {

// if file isn't this directory or its parent
if ($file != "." && $file != "..")
{
if(file_contains_special_char($file))
{
if(!unlink($directory . $file))
{
echo "ERROR, delete failed for file $file\n";
}
}
}

}

// tidy up: close the handler
closedir($handler);

?>
 
Old 10-24-2010, 03:59 PM   #5
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
A bit late, but for future reference:

ctrl-v, then press return - that'll insert you your ^M character
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
selecting all files to mv chisholm Linux - Newbie 2 11-09-2008 03:40 PM
selecting a range of files.... mjh Programming 0 03-12-2008 09:02 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM
selecting specific files slack66 Linux - Newbie 1 02-14-2004 07:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration