LinuxQuestions.org
Help answer threads with 0 replies.
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 01-24-2010, 06:12 AM   #1
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Rep: Reputation: 32
Using the CHOWN command with a wildcard.


Alright, I have a small dilemma.

I want to CHOWN a bunch of files (few thousand) to a specific owner/group. This is easy, right? Just type this.

chown owner:group *

Within' the directory all the files reside in. Well, the problem is some of the files start with a "-" thus giving the chown command an error when it reaches this file.

Code:
chown: invalid option -- 1
Try `chown --help' for more information.
I then tried to wrap the last bit of the command with quotes.

chown owner:group "*"

This doesn't work either. I even tried using single quotes, then tried getting more specific with the names DM-*, *.uz, etc... all will not work.

How can I get around the issue with the few files that start with a hyphen?
 
Old 01-24-2010, 06:31 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
add "--" before the *. doing that means that nothing following the double dash is interpretted as an option, so will work with that filename fine.
 
Old 01-24-2010, 06:33 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
chown <user>:<group> -R <directory name>
 
Old 01-24-2010, 06:33 AM   #4
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Use double hyphen: One hyphen for specifying an option ("here comes an option, parse it!"), and a second one for the option itself, which too is a hyphen. This option specifies that everything that follows is indeed a filename.

So you could do it like this:
Code:
chown -- owner:group -testfile
 
Old 01-24-2010, 06:35 AM   #5
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Hmm. Of course acid_kewpie beat me to it.
 
Old 01-24-2010, 06:36 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by catkin View Post
chown <user>:<group> -R <directory name>
Probably totally suitable here, but the OP doesn't mention wanting to do any subdirectories, so this could be confusing.
 
Old 01-24-2010, 06:40 AM   #7
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
maybe putting putting wildcard on both sides. well it worked over here chown $USER:$GROUP "*-*" check with ls before run that command
 
Old 01-24-2010, 06:44 AM   #8
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by acid_kewpie View Post
add "--" before the *. doing that means that nothing following the double dash is interpretted as an option, so will work with that filename fine.
Worked excellently. Will also help me with a script problem I've been having as well, since moving some of those file names with dashes at the beginning was disrupting the "mv" command. I take it I can apply the same method here too.

mv --
 
Old 01-24-2010, 06:52 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by acid_kewpie View Post
Probably totally suitable here, but the OP doesn't mention wanting to do any subdirectories, so this could be confusing.
Good point and thanks for pointing it out.

BTW the chown man page does not document option "--" to end options, hence my -R suggestion. Similarly its syntax summary (chown [OPTION]... [OWNER][:[GROUP]] FILE...) does not include having options after specifying the owner (so the OP example should have worked). Hmm ... ?
 
Old 01-24-2010, 06:55 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ozanbaba View Post
maybe putting putting wildcard on both sides. well it worked over here chown $USER:$GROUP "*-*" check with ls before run that command
That should not have worked because the double quotes should have prevented filename expansion and chown should have been given exactly *-* as file name to operate with. Are you sure it worked?
 
Old 01-24-2010, 06:55 AM   #11
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by catkin View Post
Good point and thanks for pointing it out.

BTW the chown man page does not document option "--" to end options, hence my -R suggestion. Similarly its syntax summary (chown [OPTION]... [OWNER][:[GROUP]] FILE...) does not include having options after specifying the owner (so the OP example should have worked). Hmm ... ?
For the record, their are no sub directories in the directory. Just a crap load of files.

I wonder if the MV command has the same thing as well. Because I get the same error with similar files if I try to move multiple files using a wild card with the mv command.

Just checked the man page for mv:

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

It could possibly think that the first source could be that of a option, if the file name begins with a dash. It also does the same if it's within' a director. Example:

Run the command from /home to move files in /home/upload/files/a

mv /home/upload/files/a/-afile.ga /home/backup/

This will return an error exactly as shown in the first post, yet the option is after directories have been declared. Strange.

Last edited by Skillz; 01-24-2010 at 06:59 AM.
 
Old 01-24-2010, 07:32 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Skillz View Post
mv /home/upload/files/a/-afile.ga /home/backup/

This will return an error exactly as shown in the first post, yet the option is after directories have been declared. Strange.
I was not able to reproduce that behaviour:
Code:
c:~/d/bin/try$ ls -l -- *file
-rw-r--r-- 1 c users 0 Jan 24 18:39 -a.file
c:~/d/bin/try$ mv ~/d/bin/try/-a.file /tmp
c:~/d/bin/try$ mv --version
mv (GNU coreutils) 7.4
[snip]
Neither ls --help output nor the ls man page describe "--" to terminate options but it works, as shown above.

The ls, mv and chown man pages' syntax summaries all show that options must be given before arguments but simple tests show all three accepting an option after an argument:
Code:
c:~/d/bin/try$ /bin/ls /tmp/*file -l
-rw-r--r-- 1 c users 0 Jan 24 18:39 /tmp/-a.file
c:~/d/bin/try$ chown c /tmp/*file -d
chown: invalid option -- 'd'
Try `chown --help' for more information.
c:~/d/bin/try$ mv /tmp/*file . -v
`/tmp/-a.file' -> `./-a.file'
removed `/tmp/-a.file'
I am surprised; I thought man pages were definitive. IIRC, UNIX (including Solaris and HP-UX) commands did not look for options after the first argument.
 
Old 01-24-2010, 09:44 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Well it's not ls specific, so doesn't belong in any ls documentation, it's just bash. Infact, is it base POSIX compliance?

Actually seems to be something inbetween - http://www.sdsc.edu/~moreland/course...topt/Long.html

Last edited by acid_kewpie; 01-24-2010 at 09:48 AM.
 
Old 01-24-2010, 10:41 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I thought it was more of a GNU thing; a lot of their tools seem to have that option where it makes sense to do so.
 
Old 01-25-2010, 09:42 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by acid_kewpie View Post
Well it's not ls specific, so doesn't belong in any ls documentation, it's just bash. Infact, is it base POSIX compliance?

Actually seems to be something inbetween - http://www.sdsc.edu/~moreland/course...topt/Long.html
Thanks for the input Chris

AFAIK, it's not bash; bash simply tokenises "words" before presenting them to the named executable and leaves the executable to parse the argument list. Bash' own getopts does not have any support for "--" but can be used to implement it (imperfectly: the error trap for "-" does not work).

EDIT: that's not right; the following code stops processing options on finding "-" whether or not followed by another "-"

Code:
#!/bin/bash

    while getopts 'h-' opt
    do
        case $opt in
            h )
                echo 'Got option -h'
                \exit 0
                ;;
            '-' )
                echo 'Got option -'
                case "$OPTARG" in
                    '-' )
                        break
                        ;;
                    * )
                        echo 'invalid option -'
                        \exit 0
                        ;;
                esac
                ;;
            * )
                echo "Invalid option '$opt'"
        esac
    done
echo 'Finished processing options; now processing arguments'
From the X Open Group's The Single UNIX ® Specification, Version 2, Utility Conventions (could not find anything later): "Guideline 10: The argument -- should be accepted as a delimiter indicating the end of options" so it is required for POSIX compliance. All the same, when it is implemented it would be nice to see it mentioned in the man page.

Netsearching did not find anything about options appearing in the arguments.

EDIT:

From the getopt(3) man page (my bolding): "By default, getopt() permutes the contents of argv as it scans, so that eventually all the non-options are at the end. Two other modes are also implemented. If the first character of optstring is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a non-option argument is encountered. If the first character of optstring is '-', then each non-option argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.) The special argument "--" forces an end of option-scanning regardless of the scanning mode."

Last edited by catkin; 01-25-2010 at 09:54 AM. Reason: Removed spurious copy-and-paste
 
  


Reply

Tags
chown, filenames



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
How to use the chown command cbjhawks Linux - Newbie 2 01-12-2010 10:10 AM
Find command to search wildcard in path? markdjones82 Linux - General 12 12-11-2009 01:02 AM
Help with chown command brokenpromises Linux - Newbie 2 09-05-2007 03:24 AM
script for a command that won't accept wildcard value hemp4fuel Programming 4 05-24-2004 05:30 AM
chown command help ksgill Linux - Newbie 9 04-28-2003 10:52 PM

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

All times are GMT -5. The time now is 11:24 PM.

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