I would like to clear out my /bin folder in my project directory. How can I do this?
I tried rm -rf ~/bin but no luck
~ is a shorthand to a current user home directory. So unless it's also your project directory you are doing something wrong. Other than that, clearing a directory would be
rm -rf ~/bin/*
And if you also want to clear the hidden files
rm -rf ~/bin/* ~/bin/.[a-zA-Z0-9]*
Make sure you are not doing
rm -rf ~/bin/.*
especially as root as it will try to clear out your entire system.
UPD
Why? Since wildcard (*) is interpreted by shell as zero or more characters of any kind the .* will also match . (current directory) and .. (parent directory), which will result in going all the way up and then down, trying to remove each file in your filesystem tree.
You should say "... my bin folder", not "my /bin folder". /bin is an absolute path, bin is a relative path.
rm -rf ~/bin removes $HOME/bin, so not what you want either.
Now, it depends on where you are: if you are in your project directory when you type the command, just type rm -rf bin.
rm -rf ~/bin/{*,.[^.]*}
would delete all files and directories in ~/bin/, including hidden ones (name starts with .), but not the parent directory (i.e. ..).
The .[^.]* matches all hidden files and directories whose name starts with a dot, the second char is NOT a dot, and with or without more chars.
Related
I'm not allowed to use rsync on the cluster I'm working on so I need to use cp. I want to copy a large directory including all files and subfolders etc. but without any folders that have the name "outdir".
I tried cp -r -v ./!(outdir) ../target-directory/
but it still copies all folders and contents in deeper directories with the name outdir. It only included the outdir folders in the highest directory.
I also tried cp -r ./*/!(outdir) ../target-directory/ but that one copied all files into the folder without keeping any hirarchy or folders etc.
I also tried certain find commands but it didn't work, but maybe I was just doing something stupid. I'm a beginner with bash so if you could explain your answer and what the flags etc. do that would really be helpfull, I've been trying forever now, on what I think shouldn't be that hard to do.
Instead of cp, you can use tar with option --exclude to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
So any path that contains the "outdir" pattern will be excluded.
Without the --exclude option, it will copy the entire structure of your current directory under TARGET_DIRECTORY.
You can replace the . in the first tar by your desired source directory.
This might appear a noob question.
While working in bash, if we run cd ., it stays in the current folder.
I understand the functionality, however, I am not able to understand the rationale of this functionality?
What would be some practical ways to use this?
The primary use case I've seen for cd . is to test whether your file handle on the current directory is still valid.
If you're on a directory from a network share -- NFS, or the like -- it can be possible for directories to be remotely deleted, but for the local client to still believe they're accessible and in use.
cd . is a way to trigger an error if your handle on the current working directory is no longer valid.
This is the only "practical" case that came to my mind
$ cd .
cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
when your process has a current working directory referencing a directory that has been removed by another process.
That command has no functionality. But in a POSIX-compliant environment, if you add a -P option, then it has functionality: it resolves symlinks. So for example on a Mac, if you cd to a path with a symlink:
cd /System/Library/Frameworks/AppKit.framework/Versions/Current
...then do cd -P . ... you will point to:
/System/Library/Frameworks/AppKit.framework/Versions/C
. is a special file that represents the current directory.
There are plenty of things which make use of directories and it is sometimes useful to refer to the current directory.
Changing the directory to the current directory is not one of those.
A simple example where cd . fails:
mkdir my_error
cd my_error
rm -rf ../my_error
cd .
When the rm is embedded in a difficult script or can be done by some other cleanup process, is can be an useful check.
I use a build script which removes and recreates a directory.
The old directory disappears and new appears with new inode.
If, in one of my shells my $PWD is that reappeared directory and I notice
it became unusable (and I know it was recreated), I just
$ cd .
to get the (new) directory useable again and can continue my work there.
I have a directory containing one single folder named .git along side various other files and folders. I want to delete all files and all folders, except for my .git directory.
Note that if any of the folders contain a subfolder named .git, I want to remove those as well. Only the root git repo should be preserved!
This is that simple as :
shopt -s extglob dotglob
rm -rf !(.git)
See http://mywiki.wooledge.org/glob
How to delete the recursive directory in Solaris.
Directory Structure : SourceCode/unit_test_cases.
The command
cp -rf SourceCode/ SourceCode/unit_test_cases/ : created a recursive directory.
Directory Structure looks like below
SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/
I know "rm -rf SourceCode" is to delete recursively. But this command gives the error File Name too long. This is due to unlimited child directories recursively created.
So am not able delete the directories.
I tried this in Ubuntu, It is smart that it determines the cyclic recursion and breaks # some depth, But Solaris seems to be dumb in this case.
Can anybody help me
It is simply
rm -rf PATHS
where PATHS is one or more PATH to be removed (it's enough SourceCode if you want to remove that dir completely).
Try this
rm -r <your directory>
and be careful of what you are deleting.
How do I remove certain files from a different directory than $PWD using the bash shell script.
Looking at the documentation for rm, it appears that rm only works in $PWD.
Am I forced to use this method:
oDir=$PWD
cd directorytoremovefiles
rm files
cd oDir
rm certainly does work for deleting files in another directory.
Whatever gave you that idea from the man page, I certainly hope it's not this:
rm removes each specified file. By
default, it does not remove
directories.
The documentation you refer to, talks only about having write & execute permission to the directory you are deleting from.
So you only need:
rm directorytoremovefiles/files
As pointed out by YYC,
rm $DIR/files
rm will take any path, relative or absolute. If there is no slash at the beginning of directorytoremovefiles then it is a relative path and you may need to store PWD for later. However, you can do this with pushd and popd or a cd - once you're finished. Or, if you run the cd and rm commands in parens they will run in a subshell, like this: ( cd directory; rm files) then your working shell will not change directory.