After executing these 4 commands, directory "a" has file "1" with contents "test" and file "2" which is empty, and directory "b" is empty.  $ mkdir a $ mkdir b $ echo test &gt; a/1 $ touch a/2   We can run  $ diff -r a b Only in a: 1 Only in a: 2   Which isn't helpful for a patch.  Or, we can run  $ diff -rN a b diff -rN a/1 b/1 1d0 &lt; test   Which is great for file "1" because we see its contents, but now file "2" has disappeared.  This is because the -N flag means "treat absent files as empty", so I presume diff can't see the difference between the empty file "a/2" and the (pretend, since really empty) empty file "b/2".  Is there a way to make diff show us, in one execution, that a new empty file has been created, and the contents of a new non-empty file -- in a format that patch can use?  Or, would this require a new option to diff?
120423
I'm trying to compare files on the root file system with a backup, and I'd like the comparison to work a bit like git or svn diff when a file has been added or removed - That is, display the full file diff. diff unfortunately just prints a No such file or directory message, which is not very useful. I couldn't find an option for this.  The simplest workaround I could find is to parse the diff output and diff -u path &lt;(printf '') or diff -u &lt;(printf '') path depending on which path exists. That's a bit of a kludge - Probably 10 lines of code for what could be a simple diff option.  Another workaround is  diff -u &lt;([ -e "$path1" ] &amp;&amp; cat "$path1") &lt;([ -e "$path2" ] &amp;&amp; cat "$path2")   , but that loses the file names in the diff.  Based on  and the previous workaround, I ended up with this:  diff -u \     "$([ -e "$path1" ] &amp;&amp; echo "$path1" || echo /dev/null)" \     "$([ -e "$path2" ] &amp;&amp; echo "$path2" || echo /dev/null)"   Edit: Turns out there is an option for it:  -N, --new-file        treat absent files as empty   In other words:  diff -Nu "$path1" "$path2"
95704
I noticed while answering another question that test and [ are different binaries, but the [ manpage pulls up test's. Besides the requirement for an ending ], is there any difference?  If not, why are they separate binaries instead of being symlinked? (They are also bash builtins, and bash doesn't show a difference either.)
83017
I have an empty directory. I am using following command to view the contents of that directory. ls -lart  The output I get is below. total 12 drwxr-xr-x 5 root  root  4096 Oct  2 12:26 .. drwxr-xr-x 2 apx   aim   4096 Nov 29 18:40 .  I don't have any files in this directory, then what counts out to 12 here (total 12)?
18208
I ran following commands in the order specified:  $ln a b $ls -i a b 523669 a 523669 b $rm -f a $ls -i b 523669 b   I concluded from this test that the command rm actually removes only the filename (a in this test) instead of the file, as the inode still exists and could be retrieved through another filename (b).  My question is, if a file is hard linked to only one filename, when rm is executed to the file, is the real file (i.e., the inode) removed completely? And if not, can a file inode be retrieved without a filename and only through the inode?
42705
When creating directories, mkdir -m &lt;mode&gt; &lt;dir&gt; provides for creating one or more directories with the given mode/permissions set (atomically).  Is there an equivalent for creating files, on the command line?  Something akin to:  open("file", O_WRONLY | O_APPEND | O_CREAT, 0777);   Is using touch followed by a chmod my only option here?    Edit: After trying out teppic's suggestion to use install, I ran it through strace to see how close to atomic it was. The answer is, not very:  $ strace install -m 777 /dev/null newfile ... open("newfile", O_WRONLY|O_CREAT|O_EXCL, 0666) = 4 fstat(4, {st_mode=S_IFREG|0666, st_size=0, ...}) = 0 ... fchmod(4, 0600)                         = 0 close(4)                                = 0 ... chmod("newfile", 0777)                  = 0 ...   Still, it's a single shell command and one I didn't know before.
18148
Say I have a directory named foo/. This folder includes subdirectories. How can I delete all the empty directories in one command?
6076
I had a directory with a file and another empty directory in it like this:  . .. file.ext folder   I did:  mv file.ext /folder   The file disappeared now, not in folder too not where it was before!   Anyway to find it back?   Thanks
105866
One might think that  echo foo &gt;a cat a | rev &gt;a   would leave a containing oof; but instead it is left empty.   Why? How would one otherwise apply rev to a?
100382
I'm having trouble with a bash script I want to make  I know ls will list files that are in a directory but I want it to list directories that are in directory1 but NOT in directory2, and then list files in directory2 that are NOT in directory1.  In a feeble attempt, I tried:  ls -al | diff directory1 directory2   Quickly I realized why it didn't work. Can anyone help out a total bash nerd out?
10337
Why do we use ./filename to execute a file in linux?  Why not just enter it like other commands gcc, ls etc...
87437
I'm trying to beat my head against the syntax of this. I'm getting problems with whitespaces and I can't see if there's a better way to do this.  vardir=/home/user cd $vardir for i in $(ls "$vardir") ; do timestamp="$(stat -c %Y $i)" echo "$i" echo "$timestamp"  done   Result is similar no matter where I quote and I'm not sure what I'm doing wrong:  stat: cannot stat ‘Temporary’: No such file or directory Temporary  stat: cannot stat ‘Items’: No such file or directory Items   My goal is I want to grab the timestamps off of backup directories and touch the current ones to restore the original modified dates.
101845
I have an unusual idea to use git as a backup system. So let's say I have a directory ./backup/myfiles and I want to back that up using git. To keep things clean I don't want to have a .git directory in the myfiles folder, so I thought I could create ./backup/git_repos/myfiles. From looking at the git docs, I've tried doing this:  $ cd backup/myfiles $ mkdir ../git_repos/myfiles $ git --git-dir=../git_repos/myfiles init Initialized empty Git repository in backup/git_repos/myfiles/ $ git --git-dir="../git_repos/myfiles/" add foo fatal: pathspec 'foo' did not match any files   You can see the error message I get there. What am I doing wrong?
86631
Non existing file  $ ls file_not_exists.txt ls: cannot access file_not_exists.txt: No such file or directory $ echo &lt;&gt; file_not_exists.txt  $ ls file_not_exists.txt  file_not_exists.txt $ cat file_not_exists.txt $   File with content  $ cat temp.txt  asdf $ echo temp.txt  temp.txt $ echo &lt;&gt; temp.txt   $ cat temp.txt  asdf    If the file does not exist, echo &lt;&gt; file_not_exists.txt will create a new file. So I think &gt; works (redirecting the empty output into a newly created file). But if there is something in the file (like temp.txt), why it is not emptied by echo &lt;&gt; temp.txt?
78834
I want to elaborate a command that would print differently simple differential dx and multidimensional differential d^3x. For this purpose I invented the command \dif with one optional argument. It is defined as follows:  \newcommand{\dif}[1][]{%     \def\@tmp@a{#1}%     \ifx\@tmp@a\@empty         [b1]\mathrm{d}     \else         [b2]\mathrm{d}^{#1}{} \!     \fi }   Here [b1] and [b2] are added for debugging. Branch [b1] is intended for the case when the command is called without any argument, e.g. \dif{x}. Second branch [b2] should be taken in case where, e.g., the command \dif[3]{x} is used to print d^3x. The definition, shown above, seems to work fine. However, if \@empty is substituted with \empty first branch is always ignored.   The command \empty is explained in The TeXBook, so that initially I tried it for this code and only occasionally reverted to \@empty having remembered a post at this site where it was said that the  \end command of Plain TeX is redefined to \@@end in LaTeX.     Can somebody explain what is the difference between \empty and   \@empty in LaTeX and, perhaps, suggest a better solution for my   problem?
20132
I was just trying to list all directories and files under current directory and also write if they are file or directory with the following command:  find -exec echo `echo {} : ;if [ -f {} ]; then echo file; else echo directory;fi` \;   I know it is a silly command, I can use other things like -type f or -type d, but I want to learn why that piece of code did not work as I expected. It just prints directory to all of them. For example while output of find is:  . ./dir ./dir/file   output of my code is :  . : directory ./dir : directory ./dir/file : directory   And output of   echo `echo dir/file : ;if [ -f dir/file ]; then echo file; else echo directory;fi`   is  dir/file : file   I am working on Ubuntu 14.10 and using find (GNU findutils) 4.4.2
45364
I have seen the following technique used many times on many different shells, to test if a variable is empty:  if [ "x$1" = "x" ]; then      # Variable is empty fi   Are there any advantages on using this over the more canonical if [ -z "$1" ]? Could it be a portability issue?
57019
Normally we use a different file to redirect the output.  For example :  cat &lt; first &gt; temp   In this command the contents of first are redirected to temp instead of the standard output.  Then why does it truncates the file if I use the same file name?  Why can't it overwrite the same file?  cat &lt; first &gt; first