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.
Related
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've been working on a script but always with a fixed directory (/opt/mw/script).
I need to change that to be able to execute the script from any directory.
I think I will need to add a "." at the beginning of the line to be able to execute the script?
For example ./mw/script
Is this correct?
Thanks
You already can execute that script from any directory with that absolute file path. It's the relative file paths (that start with ./ or ../) that can only be executed from a specific directory.
You need to add the "." if the script is in the current directory (e.g. ./script), but it is optional if the script is already inside another directory (e.g. mw/script).
Also notice that if your script contains relative references to other files and directories, you may need to use this trick to properly refer to them from any directory.
For instance, consider the following script using absolute paths:
#!/bin/bash
# lists all files in this directory
ls /opt/mw
If it is converted to relative paths as follows:
#!/bin/bash
# lists all files in this directory
ls mw
Then this script will only work if you run it from its own directory (e.g. cd /opt/ && ./script).
But then you can generalize it like this:
#!/bin/bash
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# lists all files in this directory
ls $SCRIPT_DIR/mw
Now the script works even when executed from another directory.
I want to delete a symlinked file not the symlink.
I'm running a shell script to take mysql backup's in the server and the latest backup date is symlinked to a file called latest.sql and when I try rm -rf latest.sql the symlink is get deleted not the file. Is it possible to delete the particular symliked file and the symlink?.
Any help would be appreciated.
I think the easiest way would be
rm "$(readlink linkname)"
to remove the file the link points to. The link will remain as a dangling link, so use
rm "$(readlink linkname)" linkname
to remove both. If multiple indirection is a concern, consult man readlink for the -f andd -e options.
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.
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.