redirecting - 'permission denied'

This is a discussion on redirecting - 'permission denied' within the C Programming forums, part of the General Programming Boards category; Hi, I want to redirect the output of the 'ls' command into a file. For example "ls -Fa | ./newfile.text" ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    redirecting - 'permission denied'

    Hi, I want to redirect the output of the 'ls' command into a file. For example "ls -Fa | ./newfile.text"
    However I am getting the 'permission denied' error. Am I going about this the correct way?

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well this is not the right place for this question but I think you want something like:

    ls -Fa > newfile.text

    Without more specifics that is the best I can suggest....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Thanks for the reply. I just realized that 'redirecting' is possibly the wrong word to use for the | symbol. Anyway I 'd like to stick to using '|' . Any advice would be helpful, thanks

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can't do that because:

    1) You are trying to stream the output through a pipe, which is not quite the same as simple redirection.
    2) There is no process on the other end of the pipe -- there is a non-executable file, hence "permission is denied".

    Hopefully you understand *nix file modes. These are set/read as a mask. The first part of the mask refers to the owner, the 2nd to the owner's group, the third to everyone else. The mask is:

    1: executable
    2: writable
    4: readable

    So 6 means read/write permission (4+2). 640 would mean the owner can read/write the file, the owner's group can read the file, and everyone else has no access to the file.

    Text files are usually set some combination of 6 4 and 0 because you do not execute a text file (executable scripts excepted). So if you try and execute a text file set 666 -- which means anyone can read or write it -- you will get permission denied, because the executable "bit" (1) is not set.
    Executables are usually set 7 or 5 (read + write + execute, or read + execute). 1 (execute only) is oxymoronic because a file must be read to be executed.

    But you do not want to execute newfile.text anyway, you want to write the output of ls into it. For that use:

    ls > newfile.text

    Notice, redirection uses >, not |. | is a pipe.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by MK27 View Post
    You can't do that because:

    1) You are trying to stream the output through a pipe, which is not quite the same as simple redirection.
    2) There is no process on the other end of the pipe -- there is a non-executable file, hence "permission is denied".

    Hopefully you understand *nix file modes. These are set/read as a mask. The first part of the mask refers to the owner, the 2nd to the owner's group, the third to everyone else. The mask is:

    1: executable
    2: writable
    4: readable

    So 6 means read/write permission (4+2). 640 would mean the owner can read/write the file, the owner's group can read the file, and everyone else has no access to the file.

    Text files are usually set some combination of 6 4 and 0 because you do not execute a text file (executable scripts excepted). So if you try and execute a text file set 666 -- which means anyone can read or write it -- you will get permission denied, because the executable "bit" (1) is not set.
    Executables are usually set 7 or 5 (read + write + execute, or read + execute). 1 (execute only) is oxymoronic because a file must be read to be executed.

    But you do not want to execute newfile.text anyway, you want to write the output of ls into it. For that use:

    ls > newfile.text

    Notice, redirection uses >, not |. | is a pipe.

    Thanks for that information.

    If I wanted to use the following command....

    ls -Fa | file1 (where file1 is an exe)

    what should happen? This is off topic, sorry about that

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Perhaps if you:
    1. Moved this to the Linux subforum here and...
    2. Explained what you are *really* trying to do...

    You might get some help...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by metros View Post
    Thanks for that information.

    If I wanted to use the following command....

    ls -Fa | file1 (where file1 is an exe)

    what should happen? This is off topic, sorry about that
    file1 would get the output of ls -Fa as an input stream and if the program "file1" was set up to do so, it would process it....for example:

    ls -Fa | grep foo

    would effectively be a filter, only emitting lines that contained the string "foo"...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by jeffcobb View Post
    Perhaps if you:
    1. Moved this to the Linux subforum here and...
    2. Explained what you are *really* trying to do...

    You might get some help...

    Yep will do, thanks

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by jeffcobb View Post
    file1 would get the output of ls -Fa as an input stream and if the program "file1" was set up to do so, it would process it....for example:

    ls -Fa | grep foo

    would effectively be a filter, only emitting lines that contained the string "foo"...

    Final question on this........


    So file1 needs to be set up (ie coded with input parameters or whatever) to take in the output?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by metros View Post
    So file1 needs to be set up (ie coded with input parameters or whatever) to take in the output?
    It needs to be a functioning program that process stdin, yes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by MK27 View Post
    It needs to be a functioning program that process stdin, yes.
    If anyone could point me in the direction of simple examples of how to accept input from either the command line or other files I'd appreciate it, thanks.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    As jeffcobb stated previously, please ask these questions on a Linux/Shell forum.

    This is an excellent one: Shell Programming and Scripting - The UNIX and Linux Forums

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl permission denied
    By NLFortier in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 08:31 PM
  2. tmpnam() : permission denied when try to open temp file
    By happyclown in forum C Programming
    Replies: 3
    Last Post: 03-16-2009, 11:48 PM
  3. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 11:55 AM
  4. Permission Denied
    By Morgul in forum Windows Programming
    Replies: 2
    Last Post: 06-01-2005, 04:31 PM
  5. Permission Denied error
    By ChazWest in forum C Programming
    Replies: 7
    Last Post: 03-08-2002, 04:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21