- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Operations on irregularly named files in a dir...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 04:06 AM
04-06-2011 04:06 AM
There are 10 files in a directory called:
a
b
1a
\
*
x
-
\*
9
0
what scripting code/ awk / perl would you use to (safely and robustly) delete all files that d0 not start with an alpha or numeric ?
thks in advance
Solved! Go to Solution.
- Tags:
- rm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 04:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 05:05 AM
04-06-2011 05:05 AM
Re: Operations on irregularly named files in a directory
You could do:
# cd mydirectory
# ls -1 | perl -ne 'm{^[a-zA-Z0-9]} or print'
Note that the 'ls -1' is a numeric ONE.
If you are satisfied with the output of the above, which would be the files to be *removed* then _change_ the 'print' to 'unlink'. Doing so will remove those files.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 05:49 AM
04-06-2011 05:49 AM
Re: Operations on irregularly named files in a directory
thks ...the inode solution is good but how would you script that ?
The perl is slick ...but how would one script the end bit ...to avoid
rm "*" (lol) and rm "\" ....
as a
for i in `perl stuff here`
loop would cause $i (in the case if *) to list all the files....
so doing
rm $i would effectively be doing rm "list of file" or rm *
thks !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 05:57 AM
04-06-2011 05:57 AM
Re: Operations on irregularly named files in a directory
ls -1 | perl -ne 'm{^[a-zA-Z0-9]} or unlink'
produces the same output / full file list without any deletions ..unless Im getting the syntax wrong ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 06:04 AM
04-06-2011 06:04 AM
Re: Operations on irregularly named files in a directory
OK, it's reasonable to skip the dot files :-) Your original description of your problem didn't suggest that requirement :-)
#cd /path && ls -1 | perl -ne 'm{^[.a-zA-Z0-9]} or print'
Note that I added a dot ('.') to the permitted character list.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 11:07 AM
04-06-2011 11:07 AM
Re: Operations on irregularly named files in a directory
I think it's not common to have nonprinting characters in filenames, so there's no need to script it. Anyway, I like to sleep well and wouldn't automatize (e.g. through cron) such a script that checks for such files and deletes those. I like to do this manually, that's why I said "rm -i". Just to be sure nothing important get deleted.
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 06:36 PM
04-06-2011 06:36 PM
Re: Operations on irregularly named files in a directory
I think it's not common to have nonprinting characters in filenames, so there's no need to script it.
wow Viktor ...thats a very special statement there. I bow to your superior knowledge on whats not common and what scripts are "needed" for everyone in the SA world.
;-)
...Perhaps there should be a new thread called "It happened to me" to get a sense of the odd things that have happened ..
Seriously though, the fact that the question has been asked is indicative of a "need". It only needs to occur 1ce for it to be relevant to the question asker. ...of course saying "I dont know" ...or nothing might also be an option
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 06:42 PM
04-06-2011 06:42 PM
Re: Operations on irregularly named files in a directory
yes the dot files are excluded from the list/ requirement. I do like the simiplicity of the perl solution ...I m still unclear though how you would delete the found files ....
a ls -1 | perl -ne 'm{^[.a-zA-Z0-9]} or unlink'
then an ll shows
all the (original) files *, \ etc
thks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 07:08 PM
04-06-2011 07:08 PM
Re: Operations on irregularly named files in a directory
rm -i !([A-Za-z0-9]*)
- Tags:
- pattern
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2011 08:15 PM
04-06-2011 08:15 PM
Re: Operations on irregularly named files in a directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2011 10:31 AM
04-07-2011 10:31 AM
Re: Operations on irregularly named files in a directory
> then an ll shows all the (original) files *, \ etc
Oops, try this:
# ls -1 | perl -nle 'm{^[.a-zA-Z0-9]} or unlink'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2011 11:31 AM
04-07-2011 11:31 AM
Re: Operations on irregularly named files in a directory
...and the addition of the '-l' switch is a kludge which hides a common (?) oversight.
The behavior is documented in 'perlrun' and in part says:
First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets $\ to the current value of $/ .
For every line read from our pipe, the filename already ends with a newline character. In the absence of the '-l' switch we let the print() honor what's there. That's fine for pretty printing, *but* means that 'unlink()" sees a string with the newline which does *not* match the file we think it should.
Adding the '-l' "fixes" the problem (ever so obtusely) since an automatic chomp() of the newline character occurs, leaving 'unlink()' to be handed a filename that can be found.
All this to say, it would have been much better to have written:
# ls -1|perl -ne 'chomp;m{^[.a-zA-Z0-9]} or print "$_\n"'
...to print, and:
# ls -1|perl -ne 'chomp;m{^[.a-zA-Z0-9]} or unlink'
...to actually remove files.
Or, if you prefer to be a a bit long-handed, say:
' ... or unlink $_'
By the way, unlink() will normally not remove directories.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2011 08:49 PM
04-08-2011 08:49 PM
Re: Operations on irregularly named files in a directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2011 08:52 PM
04-08-2011 08:52 PM