[solved]Locked output file from a Linux command

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read this

[solved]Locked output file from a Linux command

Postby James_patageul on Tue May 14, 2013 3:52 pm

Hi people,

I want the content result of a command line output to a file, I have found the solution not too complicated.

so here is my command ->
Code: Select all
su
netsat -lnp | grep citserver | >/var/tmp/output.txt


but when I go to the location var\tmp\output.txt I see that ->
Image
so it seem the file is locked and the content seem empty.

did I must launch caja (the file browser) as SuperUser ?

thx
Last edited by James_patageul on Wed May 15, 2013 11:27 am, edited 1 time in total.
Linux Mint \ Debian \ 201303 MATE 64-bit
User avatar
James_patageul
Level 1
Level 1
 
Posts: 4
Joined: Tue May 14, 2013 3:40 pm

Linux Mint is funded by ads and donations.
 

Re: Locked output file from a Linux command

Postby xenopeek on Tue May 14, 2013 3:58 pm

Because you first run su, you are running the netstat command as the root user. Hence the file you create is owned by root.

Either change ownership of the file back to yourself, which you also need to do as root (with the chown command), or instead of using su use sudo. As in:
Code: Select all
sudo netstat -lnp | grep citserver > /var/tmp/output.txt

The redirection of the output is done by your shell, before the sudo command is executed. So redirection of output is done as yourself.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14552
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Locked output file from a Linux command

Postby DrHu on Tue May 14, 2013 4:00 pm

/var is a system directory and the output is there except you need root mode (su or sudo) to see it

Or you could simply dump the data to your /home directory, or even /tmp (instead of /var/tmp) you have access to /tmp

Linux FHS..
http://www.meabi.com/linux-fhs-filesyst ... -standard/
https://www.linux.com/learn/tutorials/4 ... tandard-30
User avatar
DrHu
Level 16
Level 16
 
Posts: 6611
Joined: Wed Jun 17, 2009 8:20 pm

Re: Locked output file from a Linux command

Postby James_patageul on Tue May 14, 2013 4:20 pm

thank for your fast reply !

DrHu I have try to export either inside /home or even /home/myself

but owner stay root and therefore I cannot delet it (normaly) and the content seem empty
what is strange is inside /home the file of not the lock and under
/home/myself he have the lock !?

xenopeek Yeah with sudo the owner is myself ! but what is weird is that the text file is still empty ! and with out the > /home/myself/output.txt I have a result on the terminal !
Linux Mint \ Debian \ 201303 MATE 64-bit
User avatar
James_patageul
Level 1
Level 1
 
Posts: 4
Joined: Tue May 14, 2013 3:40 pm

Re: Locked output file from a Linux command

Postby xenopeek on Tue May 14, 2013 4:25 pm

It will be empty if the grep command returns no matches.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14552
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Locked output file from a Linux command

Postby James_patageul on Tue May 14, 2013 4:30 pm

but it return results ->
Image

EDIT: OK I found why ! -> next post
Linux Mint \ Debian \ 201303 MATE 64-bit
User avatar
James_patageul
Level 1
Level 1
 
Posts: 4
Joined: Tue May 14, 2013 3:40 pm

Re: Locked output file from a Linux command

Postby James_patageul on Tue May 14, 2013 4:36 pm

OK When I enter this command

sudo netstat -lnp | grep citserver | > /home/virtua/output.txt

I have a empty file

but when I enter

sudo netstat -lnp | grep citserver > /home/virtua/output.txt (without the second | )

then is working :roll: damnn I'm such a noob :mrgreen:

could you explain me what do exactly the | ?

(I'm new the linux system (normally I admin windows server :oops: )
Linux Mint \ Debian \ 201303 MATE 64-bit
User avatar
James_patageul
Level 1
Level 1
 
Posts: 4
Joined: Tue May 14, 2013 3:40 pm

Re: Locked output file from a Linux command

Postby xenopeek on Tue May 14, 2013 4:39 pm

The '|', aka pipe operator, attaches the stdout of the left process to the stdin of the right process. So what one prints as output, the other will read as input. The '>', aka redirection operator, redirects the output to a file. Combining them might indeed not work :wink:

Perhaps a good resource for catching up a bit on the terminal: http://www.flossmanuals.net/command-line/. It covers both redirecting and piping commands.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14552
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Newbie Questions

Who is online

Users browsing this forum: No registered users and 4 guests