I would like to check if the file that a filedescriptor points to is deleted in Bash (linux). I have read both  and . But those answers did not help in this slightly different question. I use the following testcase: # create file echo hello &gt; /tmp/test.txt  # open read-only fd exec 3&lt; /tmp/test.txt  # delete file rm /tmp/test.txt  # special zero-timeout to check if data available for reading if read -u 3 -t 0 then     echo &quot;data available for reading&quot; else     echo &quot;no data available&quot; fi  # close fd (clean up) exec 3&lt;&amp;-  This script surprisingly indicates that &quot;data available for reading&quot;. However, the file does not exist anymore. So there has to be some caching/buffering going on. Perhaps there is another way, or to avoid the buffer/cache? An alternative that does work, is: ls -l /proc/$$/fd/3 which will indicate  -&gt; '/tmp/test.txt (deleted)'. But I would prefer to stick to a pure Bash solution (without spawning too many new processes, or parsing stdout). Note that in any other circumstance, one could of course just use [ -e /tmp/test.txt ] to check. However, I need to know if the original file was deleted, because meanwhile a new file with the exact same filename may have been created. For those that wonder why anyone would need this particular result (), it can be used to safely check from a subshell (with &amp;) if the parent script is still running by opening an extra fd to /proc/$$/cmdline with protection against a collision with a recycled PID.
121409
My script (should) acts differently, depending on the presence of the data in the input stream. So I can invoke it like this:  $ my-script.sh   or:  $ my-script.sh &lt;&lt;-MARK     Data comes...     ...data goes. MARK   or:  $ some-command | my-script.sh   where two last cases should read the data, while first case should notice the data is missing and act accordingly.  The crucial part (excerpt) of the script is:  #!/bin/bash local myData; read -d '' -t 0 myData;  [ -z "${myData}" ] &amp;&amp; {     # Notice the lack of the data. } || {     # Process the data. }   I use read to read input data, then option -d '' to read multiple lines, as this is expected, and the -t 0 to set timeout to zero. Why the timeout? According to  (typing left unchanged; bold is mine):     -t timeout time out and return failure if a complete line of input is               not read withint TIMEOUT seconds.  The value of the TMOUT               variable is the default timeout.  TIMEOUT may be a               fractional number.  If TIMEOUT is 0, read returns success only               if input is available on the specified file descriptor.  The               exit status is greater than 128 if the timeout is exceeded   So I in case 2 and 3 it should read the data immediately, as I understand it. Unfortunately it doesn't. As -t can take fractional values (according to above man page), changing read line to:  read -d '' -t 0.01 myData;   actually reads the data when data is present and skips it (after 10ms timeout) if it is not. But it should also work when TIMEOUT is set to real 0.  Why it actually doesn't? How can this be fixed? And is there, perhaps, alternative solution to the problem of "act differently depending on the presence of the data"?  UPDATE  Thanks to @Isaac I found a misleading discrepancy between quoted on-line version and my local one (normally I do not have locale set to en_US, so help read gave me translation which I couldn't paste here, and looking up for on-line translation was faster than setting new env---but that caused the whole problem).  So for 4.4.12 version of Bash it says:     If TIMEOUT is 0, read returns               immediately, without trying to read any data, returning               success only if input is available on the specified               file descriptor.   This gives a little bit different impression than "If TIMEOUT is 0, read returns success only if input is available on the specified file descriptor"---for me it implied actually an attempt to read the data.  So finally I tested this and it worked perfectly:  read -t 0 &amp;&amp; read -d '' myData;   The meaning: see if there's anything to read and if it succeed, just read it.  So as to base question, the correct answer was provided by Isaac. And as to alternative solution I prefer the above "read &amp;&amp; read" method.
47018
I use the following command in order to delete only the files that start with DBG and older then two day , but this syntax not print the files that deleted  find /tmp  -type f -mtime +2 -name "DBG*" -exec rm {} \;   How to add to this the find syntax , the print in order to print the deleted files?
101780
Is there a standard (POSIX) way of asking the user some data from within a shell script, with  for example, while allowing live edition of the text being typed (what readline does)?   I know bash has read -e varname that allows for the person launching the script to use keyboard arrows for exemple, to edit or correct what has just been typed without deleting the last entered characters with backspace.   However, read -e is . And still, it is pretty cumbersome to delete all that has been wrote if you realise you made a mistake at the beginning of your long sentence...
55988
I am trying to read user and server details from file tempo.txt and then check the disk space usage of the file system on that unix account using another script server_disk_space.sh.But I am not able to figure out why while loop is only working for first line and for loop is working fine.Please help me understand this.  Using while loop  #!/usr/bin/ksh while read line do r1=`echo $line | cut -d"#" -f1`; r2=`echo $line | cut -d"#" -f2`; apx_server_disk_space.sh $r2 $r1 done&lt;tempo.txt   Output  8     Using for loop  #!/usr/bin/ksh for line in $(cat tempo.txt) do r1=`echo $line | cut -d"#" -f1`; r2=`echo $line | cut -d"#" -f2`; apx_server_disk_space.sh $r2 $r1 done   Output  8 23 54 89 12     Contents of server_disk_space.sh  #!/usr/bin/ksh user=$1 server=$2 count=`ssh ${user}@${server} "df -h ."` echo ${count} | awk '{print $12}' | tr -d %   Above script outputs the value of Use percentage of Disk Usage on any server .    Contents of tempo.txt  abclin542#abcwrk47# abclin540#abcwrk1# abclin541#abcwrk2# abclin543#abcwrk3# abclin544#abcwrk33#
4546
From:  How do I modify this to only delete the first version of the file it sees.  Open Terminal from Spotlight or the Utilities folder Change to the directory (folder) you want to search from (including sub-folders) using the cd command. At the command prompt type cd  for example cd ~/Documents to change directory to your home Documents folder At the command prompt, type the following command:   find . -size 20 \! -type d -exec cksum {} \; | sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | uniq -d | grep -hif – /tmp/f.tmp &gt; duplicates.txt   This method uses a simple checksum to determine whether files are identical. The names of duplicate items will be listed in a file named duplicates.txt in the current directory. Open this to view the names of identical files There are now various ways to delete the duplicates. To delete all the files in the text file, at the command prompt type:   while read file; do rm "$file"; done &lt; duplicates.txt
111306
I want to remove certain files using find and -exec. But unlikely bash tells me, that I'm "missing" some argument.   find . -name *.png -exec rm {} /;   what do I miss?  same "missing argument" return for my attempt to rename some files:  find . -name ic_launcher.png -exec mv {} other_name.png /;   Can somebody tell me, what bash does miss, and why this command isn't successful?
56892
I want to reopen the stdin and stdout (and perhaps stderr while I'm at it) filehandles, so that future calls to printf() or putchar() or puts() will go to a file, and future calls to getc() and such will come from a file.  1) I don't want to permanently lose standard input/output/error. I may want to reuse them later in the program.  2) I don't want to open new filehandles because these filehandles would have to be either passed around a lot or global (shudder).  3) I don't want to use any open() or fork() or other system-dependent functions if I can't help it.  So basically, does it work to do this:  stdin = fopen("newin", "r");   And, if it does, how can I get the original value of stdin back? Do I have to store it in a FILE * and just get it back later?
8450
In Bash, suppose I visit a directory, and then another directory. I would like to copy a file from the first directory to the second directory, but without specifying the long pathnames of them. Is it possible?   My temporary solution is to use /tmp as a temporary place to store a copy of the file. cp myfile /tmp when I am in the first directory, and then cp /tmp/myfile . when I am in the second directory. But I may check if the file will overwrite anything in /tmp.  Is there something similar to a clipboard for copying and pasting a file?
39916
Observation: I have an executable named foo, located in /b/foo. It is compiled against an old header of a dynamic library, causing it to segfault when executed:  $ foo Segmentation fault. // Expected behaviour.   Now, I compile a new version of foo to /a/foo against the new dynamic library that should execute just fine. Directory a/ is in my $PATH before b/, so /a/foo should be selected:  $ which foo /a/foo   When I execute foo, the following happens:  $ foo Segmentation fault.   Therefore, it seems that /b/foo gets executed, whereas "which" tells me that /a/foo should be executed. To make things weirder, when I run the full path $(which /a/foo), things run fine:  $ /a/foo OK!  $ cp /a/foo . $ ./foo OK!     To go yet one step further, if I now delete /a/foo:  $ rm /a/foo   Then /b/foo must surely be chosen, right?  $ which foo /b/foo $ foo bash: /a/foo: No such file or directory $ $(which foo) Segmentation fault. // Expected result.   Nope!  Fix: Source .bash_profile and .bashrc and the problem disappears.  Reproducibility: Every time. Just remove /a/foo, source ~/.bash_profile, create /a/foo, and the above observation re-occurs.  Question: Does anyone have an idea what went wrong here?  Hypothesis: "which" is up-to-date, but the system chooses based on "what was used to be the case". In my example above, /a/foo did not yet exist when the terminal was opened: I had only just created it. Therefore, when /a/foo was created, "which" did detect /a/foo, but the system still chose /b/foo, because it was somehow out of sync? But why is the system out of sync?
53573
Basically, I would like to check if I have rights to open the file before I actually try to open it; I do not want to use a try/catch for this check unless I have to. Is there a file access property I can check before hand?
11442
I have an application that writes information to file. This information is used post-execution to determine pass/failure/correctness of the application. I'd like to be able to read the file as it is being written so that I can do these pass/failure/correctness checks in real time.  I assume it is possible to do this, but what are the gotcha's involved when using Java? If the reading catches up to the writing, will it just wait for more writes up until the file is closed, or will the read throw an exception at this point? If the latter, what do I do then?   My intuition is currently pushing me towards BufferedStreams. Is this the way to go?
63794
Possible Duplicate:          Is there a command in Linux which displays when the file was created ? I see that ls -l gives the last modified time, but can I get the created time/date?
38314
touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2&gt; /dev/null   where foo is a directory  It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' and putting a directory_ in front so the directory  /home/test/foo would create a file directory_\home\test\foo  It does exactly what I want except a warning keeps printing tr: warning: an unescaped backslash at end of string is not portable   a. I don't know what that means b. I would like it to not show  I tried to redirect stderr to /dev/null but it doesn't work. How do I suppress it?
84326
I am trying to find how to log a specific instantiation of rrdtool to see whether the path it is receiving is incorrect.  I know I could wrap the executable in a shell script that would log the parameters, but I was wondering if there was a more kernel-specific way to monitor for that, perhaps a filesystem callback that sees when a particular /proc/pid/exe matches a given binary?
37878
I somehow managed to create a file that doesn't seem to have a filename. I found some information regarding how to get more details of the file in the following   However, I tried some of the suggestions listed and can't seem to delete the file. I'm not sure what I did to create it, but it happened while trying to copy an xml file.   Some info on the file is as follows;  &gt; ls -lb total 296 -rw-r--r--   1 voyager  endeavor  137627 Jan 12 12:49 \177  &gt; file * :               XML document  &gt; ls -i  417777      I tried to find using the inum switch and then pipe that to rm as that seemed like the most foolproof way of getting rid of it. However, the example given at the bottom of the thread linked below failed for me. Example was:  &gt; find -inum 41777 -exec ls -al {} \; find: illegal option -- i find: [-H | -L] path-list predicate-list   so I tried using the path list first like the following, but that didn't work either:  &gt; find . -inum 41777 -exec ls -al {} \;   I'm not sure what the non-printable character \177 is or how I can pass that to an rm command, but I really want to make sure I don't mess up any other files/directories in my attempt to delete this file.
100532
The runtime arguments are as follows: $1 is the path to the file containing the list of files $2 is the path to the directory containing the files What I want to do is check that each file listed in $1 exists in the $2 directory  I'm thinking something like:  for f in 'cat $1' do if (FILEEXISTSIN$2DIRECTORY) then echo '$f exists in $2' else echo '$f is missing in $2' sleep 5 exit fi done   As you can see, I want it so that if any of the files listed in $1 don't exist in the directory $2, the script states this then closes. The only part I can't get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ] but I don't know how you can make sure its checking that it exists in the $2 directory.
27066
I have a requirement, if I execute a script ./123 with an arguments of empty path, say /usr/share/linux-headers-3.16.0-34-generic/.tmp_versions(this directory is empty). It should display "directory is empty"  My code is:  #!/bin/bash dir="$1"  if [ $# -ne 1 ] then     echo "please pass arguments"  exit fi  if [ -e $dir ] then printf "minimum file size: %s\n\t%s\n" \  $(du $dir -hab | sort -n -r | tail -1)  printf "maximum file size: %s\n\t%s\n" \  $(du $dir -ab | sort -n | tail -1)  printf "average file size: %s" du $dir -sk | awk '{s+=$1}END{print s/NR}' else    echo " directory doesn't exists" fi  if [ -d "ls -A $dir" ]  then     echo " directory is  empty" fi   I have an error displays like, if I execute the script name ./123 /usr/src/linux-headers-3.16.0-34-generic/.tmp_versions (this directory is empty).  minimum file size: 4096     /usr/src/linux-headers-3.16.0-34-generic/.tmp_versions maximum file size: 4096     /usr/src/linux-headers-3.16.0-34-generic/.tmp_versions average file size: 4   instead of showing output only "directory is empty" its shows the above output  The below output has to be display if I exceute the script with correct arguments( I mean with correct directory path). say ./123 /usr/share  minimum file size: 196         /usr/share     maximum file size: 14096         /usr/share     average file size: 4000   my expected output is: ./123 /usr/src/linux-headers-3.16.0-34-generic/.tmp_versions  directory is empty.