I'm trying to create a Bash script that will delete everything in my .waste directory. I have a basic script I wrote but I want it to first check if the .waste directory has contents, and if so, to echo out a simple "Folder already empty!" message. I'm not too savvy about if and if else statements, and I don't know what the [ ] equation needs to check for presence.  Basic code:  #! /bin/bash echo "The files have been deleted:" cd /home/user/bin/.waste/ ls rm -rf /home/user/bin/.waste/*   (P.S. not sure if the asterisk is correct at the end, I did try the script with it and I recall it deleted everything in the bin directory as well)
17409
From a shell script, how do I check if a directory contains files?  Something similar to this  if [ -e /some/dir/* ]; then echo "huzzah"; fi;   but which works if the directory contains one or several files (the above one only works with exactly 0 or 1 files).
11957
For example, if passed the following:  a = []   How do I check to see if a is empty?
13832
I have a string in Bash: string=&quot;My string&quot;  How can I test if it contains another string? if [ $string ?? 'foo' ]; then   echo &quot;It's there!&quot; fi  Where ?? is my unknown operator. Do I use echo and grep? if echo &quot;$string&quot; | grep 'foo'; then   echo &quot;It's there!&quot; fi  That looks a bit clumsy.
21282
I have a folder containing a lot of folders and different files with the following structures :  NASA │ ├── a │   ├── doc1 │   ├── doc2 │   ├── doc3 │   ├── folder1 │   └── folder2 │ ├── b │   ├── doc1 │   ├── doc2 │   ├── doc3 │   ├── folder1 │   └── folder2 │ ├── c │   ├── doc1 │   ├── doc2 │   ├── doc3 │   ├── folder1 │   └── folder2 │ ├─ x ├─ y └─ z          I want to delete the content of the folder (NASA/) except specified folders and files.   For example I want to keep a folder, b folder and x file.     I tried   :   rm !(a/) -r NASA/   And (as explained in the answer ):  find NASA/ -type f ! -iname "x" -delete   But this is not very straight forward and I have to use a bash script.    Am I missing a more easy way ? How can I do this in a single command?
97576
I have this code that checks that the string contains only space and hash characters (“#”), if so it echos 'yes' if not echos 'no' string=&quot;###############         #             #         # #############         #             #         # ######### # #         #         # # #         # ### ##### # #         #   #     # # #         # # ###########         # #                     ###############&quot;                          confirm_variable=&quot;Yes&quot;  for (( i=0; i&lt;${#string}; i++ )); do     str=&quot;${string:$i:1}&quot;     if [ &quot;$str&quot; == &quot;#&quot; ] || [ &quot;$str&quot; == &quot; &quot; ] || [ &quot;$str&quot; == &quot;\n&quot; ] ;          then             continue         else             confirm_variable=&quot;No&quot;             break     fi  done echo $confirm_variable  I am not sure why this isn't working as if I make the string equal something like: string=&quot;## #### # #  #&quot;   it seems to work fine.
42446
I have a directory that I am trying to clean out that contains both files and subdirectories.  What I am trying to do is simple: move all the files into another directory, but leave all the sub-directories as they are.  I am thinking something like:  mv [*_but_no_dirs] ./other_directory   It seems like there should be a simple way to do this with wildcards * and regex...  Anyone have ideas?
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.
20691
I have a directory that contains the following:  x.pdf y.zip z.mp3 a.pdf   I want to delete all files apart from x.pdf and a.pdf. How do I do this from the terminal? There are no subdirectories so no need for any recursion.
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.
104573
Even when /tmp has no file called something, searching for it with find will return 0:    $ find /tmp -name something    $ echo $?   0   How can I get a non-zero exit status when find does not find anything?
8430
I'm not well versed in shell scripting but I imagine this to be a painfully easy fix but its eluding me.  I need to copy the contents of a directory excluding a number of files. I have a command that works when I enter it into the terminal, but when its run in a shell script it fails with a syntax error or just doesn't work.   The command in question is:  cp -r path/to/!(dir1|dir2|dir3) destination   Any help would be greatly appreciated!
110327
I have separate scripts for tasks in bash. Here is the broken one:  #!/bin/bash PATH=/home/name/ mkdir $PATH cd $PATH &amp;&amp; echo "done." exit 0   Today it broke and first time it simply didn't want to run cd, but created directory. Second time it just said "mkdir command not found." Running this commands exactly with semicolon works fine. What the case?
14491
I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor  echo -e "#!/bin/bash \n /usr/bin/command args"  &gt; .scripts/command   The output:     bash: !/bin/bash: event not found   I've isolated this strange behavior to the .  $ echo ! !    $ echo "!" bash: !: event not found  $ echo \#! #!  $ echo \#!/bin/bash bash: !/bin/bash: event not found    Why is bang causing this?   What are these "events" that bash refers to?     How do I get past this problem and print "#!/bin/bash" to the screen or my file?
51790
I have a bash shell script that loops through all child directories (but not files) of a certain directory.  The problem is that some of the directory names contain spaces.    Here are the contents of my test directory:  $ls -F test Baltimore/  Cherry Hill/  Edison/  New York City/  Philadelphia/  cities.txt   And the code that loops through the directories:  for f in `find test/* -type d`; do   echo $f done   Here's the output:   test/Baltimore test/Cherry Hill test/Edison  test/New York City test/Philadelphia   Cherry Hill and New York City are treated as 2 or 3 separate entries.  I tried quoting the filenames, like so:  for f in `find test/* -type d | sed -e 's/^/\"/' | sed -e 's/$/\"/'`; do   echo $f done   but to no avail.  There's got to be a simple way to do this.    The answers below are great.  But to make this more complicated - I don't always want to use the directories listed in my test directory.  Sometimes I want to pass in the directory names as command-line parameters instead.  I took Charles' suggestion of setting the IFS and came up with the following:  dirlist="${@}" (   [[ -z "$dirlist" ]] &amp;&amp; dirlist=`find test -mindepth 1 -type d` &amp;&amp; IFS=$'\n'   for d in $dirlist; do     echo $d   done )   and this works just fine unless there are spaces in the command line arguments (even if those arguments are quoted).  For example, calling the script like this: test.sh "Cherry Hill" "New York City" produces the following output:   Cherry Hill New York City
20912
This will notify us if the file is empty:  [[ ! -s $file ]] &amp;&amp; echo "hello there I am empty file !!!"   But how to check if a file has blank spaces (spaces or tabs)?   empty file can include empty spaces / TAB
79113
I'm trying to make a bash script that would make a series of directories  and requesting a parameter of how many directories should be created.  $&gt; ./createDir.sh 5  $&gt; ls  ex_01 ex_02 ex_03 ex_04 ex_05   I tried using mkdir ex_{01..$1} but it does not seem correct. How could I make this work (without using any loop)?
26761
I was viewing my .bash_profile file, and I saw this written inside a block of if statement:  if [ -f ~/.bashrc ]; then        . ~/.bashrc    Can someone explain what the second line is actually doing?