sometimes using rm -rf in script could be catastrophic  example  rm -rf $TMP/$folder   when TMP and $folder are without any value , it will actually   removing all under "/"  so I am asking how to use the rm -rf in the scripts so it will be safe?
124229
Sometimes I use, $PROJECT_HOME/* to delete all files in the project. When the environment variable, PROJECT_HOME is not set (because I did su and the new user doesn't have this environment variable set), it starts deleting all files from the root folder. This is apocalyptic.  How can I configure bash to throw error, when I use an undefined environment variable in the shell?
89258
I want to delete the content of a folder without deleting a folder: all files and all sub folders with sub files. This doesn't delete anything  sudo rm -rf /folder1/*   Why not? How to get it to work?
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?
48925
It's hard to search for special characters on google, which is why I'm posting this here.  I recently ran (as root) a mv /tmp/folder/* /* when I meant to run mv /tmp/folder/* ./ Big mistake!  Things broke! A lot of files got mixed up and placed into strange places, none of the normal unix commands worked (ls, dir, cat, etc) and once you logged off, you couldn't ssh back in -- it would give you a prompt, but not accept the old passwords.  Running ls /* shows me that it performs ls on every directory in /. If I tried to draw a parallel, then mv should have simply moved (or copied?) the files to every directory in /. Instead, really weird things happened, and I was just curious what exactly was going on.  More info: The files in /tmp/folder had been transferred from a windows machine, and had been named file (#).txt, where (#) is a number up to two digits, with a space between file and the parenthesis. There were ~40 of these text files.
8872
I've got a folder full of folders, and some of those folders may or may not have folders in them. It looks something like this:  ~/stuff/01.d/01/files ~/stuff/02.d/files ~/stuff/03.d/files ~/stuff/04.d/lol/files ~/stuff/05.d/files   I want to make separate .rars for each of the folders in ~/stuff, so I was going to do for f in *.d; do rar a "${f//.d/.rar}" "$f"; done which worked great, and gave me the rars. But I'd rather have no directory structure in the rars at all, just files. Is there an easy way to do this? It seemed like one of the switches looked like it might have done what I wanted, but the man page is a bit confusing. I'll try a few things.  I may end up trying to figure out a bash script to cd into the directory, check if it's the files, if so make the .rar in the parent (../${f//.d/.rar}), else go into the directory again and make the rar two parents up (../../${f//.d/.rar}). In this case, I'd want rar a ../"${f//.d/.rar}" * I think, I haven't tested it.
101998
I see the different folders bin, dev, mnt, proc, srv, sys, etc.   What are the properties of all these different folders? What are their purposes? Are there files in some of them that I should not touch? Should I be touching files in the other folders?  Final question: in a Unix environment, do folders == directories? I apologize for all the questions.
111234
I am trying to remove all files and subdirectories in a directory. I used rm -r to remove all files, but I want to remove all files and subdirectories, excluding the top directory itself.  For example, I have a top directory like images. It contains the files header.png, footer.png and a subdirectory.  Now I want to delete header.png, footer.png and the subdirectory, but not images.  How can I do this in linux?
123724
I have done some research about this on Google, but the results were cloudy. Why is the / sign used to denote the root directory. Are there any solid reasons behind it?
32697
I accidentally made a directory "~/" under a subdirectory in my home directory. How to safely remove this directory without affecting my home directory. rm -r ~ obviously won't work... Thanks!
41756
I'm trying to compress a folder (/var/www/) to ~/www_backups/$time.tar where $time is the current date.  This is what I have:  cd /var/www &amp;&amp; sudo tar -czf ~/www_backups $time"   I am completely lost and I've been at this for hours now. Not sure if -czf is correct. I simply want to copy all of the content in /var/www into a $time.tar file, and I want to maintain the file permissions for all of the files. Can anyone help me out?
52441
I routinely use bind mounts to aid in making space available in multiple locations without having to have multiple logical volumes / physical partitions / LUNs, etc.  For example, I may have a 200G LV mounted at /space. From there, I will make subdirectories like var_opt and var_log which I can then bind mount to /var/opt and /var/log, respectively.  When doing a cleanup on the 'space' directory, is it possible to exclude directories from an rm -rf running inside /space?  Example:  # pwd /space # rm -rf * {except-for-var_opt-and-var_log}   Is there a different or better (but similarly simple) way to accomplish what I'm trying to do that I haven't thought of?
49924
I put together a script to do some file operations for me. I am using the wild card operator * to apply functions to all files of a type, but there is one thing I don't get. I can unzip all files in a folder like this  unzip "*".zip   However, to remove all zip files afterward, I need to do   rm *.zip   That is, it does not want the quotation marks. The unzip, on the other hand, does not work if I just give it the * (gives me a warning that "files were not matched").   Why is this different? To me, this seems like the exact same operation. Or am I using the wild card incorrectly?   in Unix do not really go into this, and I could not locate anything in the rm or zip docs.  I am using the terminal on a Mac (Yosemite).
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?
10337
Why do we use ./filename to execute a file in linux?  Why not just enter it like other commands gcc, ls etc...
46230
Is it possible to run my scripts located in ~/scripts from anywhere?  Say i want to run ./scriptname regardless of my current working directory.
62643
Why is /tmp a symlink to /private/tmp on Mac OS X? In other words, why isn't /tmp just a regular directory, like on Linux or BSD? I understand how it works and I don't mind it, I'm just interested in the (historical?) reasoning behind it.
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?