I'm currently going through this tutorial: https://learnpythonthehardway.org/book/appendix-a-cli/ex11.html Even after going over it, I couldn't figure out how to move a file up one directory.
I'm currently in testdirectory1.
I created a subdirectory subd3.
I successfully moved a file called testfile1copy.py into subd3 via mv file1copy.py subd3/
Now, how do I move that file back to testdirectory1?
mv subd3/file1copy.py .
. references the current directory. If $PWD is the full path of testdirectory1 (some shells will automatically set that variable for you), you could also do:
mv "$PWD/subd3/file1copy.py" "$PWD"
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.
So I want to copy some files from one directory to another.
Essentially, I want to capture the directory path to a variable, say, "pathname" and use
"cp-r $pathname ." to copy file1 and file2 to to a new folder in which I made using mkdir and have cd'ed into (hence the "." as the second command line argument).
source_to_copy_from:
home/folder1/folder2/file1
home/folder1/folder2/file2
destination_to_copy_to:
home/newfolder1/newfolder2/file1
home/newfolder1/newfolder2/file2
I did:
pathname=$"$(pwd)"
//code to make the newfolder1 here
cp -r $"$pathname" .
But there appears to be nothing in the new pathname that was supposed to be copied in.
Also am using Mac bash
also quite beginner to bash
Im having trouble in Bash.
I have plain files in a directory on my desktop. I am trying to move them into a subdirectory within that directory using: mv "Filename" /"Directoryname"
However when I use this command, I get an error telling me that the permission was denied.
I am set as the owner of both directories and have should have full permissions. If there is anything I need to provide you to make it easier for you to help me, I will be glad to help.
Try mv filename subDirectoryName/.
By placing / in front of the directory name in a move sequence, you're telling the shell that you would like it to be placed in a high level folder named /folder.
What you want is a sub-directory within your current directory. As you would usually move directories in bash, ../ goes up one directory, and directory/ implies you are moving into a folder that is within your current directory.
I have written following script to rename all the files within all the folders of the current directory. But I got into problem because its not only renaming the files within folder but also all the folders in current directory and parent directories in hierarchy. I realised that this is because of . and .. present in current directory. But how to get rid of them ?
for i in *
do
cd $i
for j in *
do
mv $j $i$j
done
cd ..
done
The problem is, I have many folders and they contain images that are named as image0001 to image0100. And I want to copy all of the images to one folder. So they are overwriting each other. That is why I want to rename the images.
I think what's going wrong is probably that cd $i is sometimes failing (perhaps because $i is not a directory? or perhaps because it contains spaces, so triggers word splitting?), so then you stay in the same directory, and then cd .. moves you up a directory.
To fix this (and other potential issues that could crop up), I recommend making your script a bit more cautious:
for dir in */ ; do
pushd "$dir" || continue
for file in image???? ; do
mv "./$file" "${dir%/}$file"
done
popd
done
I have written a bash script which is dependent on current folder structure,
What should I do to make it runnable in any other folder(become portable)?
let me explain more: I want the script to delete all the files in current directory that the script is running,
or to delete all the files that are in the parent folder,
what is the solution?
cd "$(dirname "$0")"
Add this to the top of your script to have it cd to the directory the script is installed at. Then you can use relative paths and they'll be relative to the script dir and not the user's pwd.
If for whatever reason dirname isn't available, here's an equivalent statement that uses basename instead.
cd "${0%%/$(basename "$0")}"
Don't use absolute paths in your script.
we need more information to give you a proper answer...BUT some tips if you need to make it portable are to store any files that you need on a network share and simply make a new folder off of / such as /temporaryScriptFolder/ and mount the network share to that empty folder, allowing you easy access to any resources that are necessary.