Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
When i today accessed my Ubuntu 16.04 server and wanted to remove the file "test2" it was simply not deleted!
I have used
rm test2
as well as
rm -f test2
but it still did not delete it as you can read here:
root#icinga:~# ls
basket desd.save packages scripts src test2 test5 unused
root#icinga:~# rm test2
root#icinga:~# ls
basket desd.save packages scripts src test2 test5 unused
root#icinga:~# rm -f test2
root#icinga:~# ls
basket desd.save packages scripts src test2 test5 unused
I have also tried to remove other files, didn't work!
I am the owner of "test2" and using ls -la test2 you can see that I have the rights to read and write this file!
root#icinga:~# ls -la test2
-rw-r--r-- 1 root root 9 Nov 11 20:33 test2
Using which rm it says /bin/rm.
root#icinga:~# which rm
/bin/rm
And also \rm test2 does not delete the file!
I have also checked for the name, there are no spaces at the end etc. because when I use cat test2 the correct content is shown!
I also can create a new file but can't delete this as well.
rm is also not an alias, I used unalias rm but it said "rm: not found".
Reboot did also not help.
I had the problem that I accidently deleted a file instead of moving it, so I created a script that simply moves the file to a certain directory.
Then I used nano /etc/environment and added ":/root/scripts" where this script was located!
After that i created the alias rms by using alias rms='./rm'. I know it might be dumb naming a file like a system command, I already changed it to remove!
But after doing all this there was the Error that rm can't be found and can be found in the following packages: coreutils. So i tried apt-get install coreutils but it said it is already installed.
So I first used touch /bin/rm and then chmod +x /bin/rm.
After that this problem occured!
EDIT: the problem was the /bin/rm file was empty so I set up an virtual machine and copied the required file to the server!
To remove a file, you need to be able to modify (write) in the directory that contains the file. If the file isn't deleted, then you probably don't have permission to write on the directory. This could be because the file is on a read-only file system, but it is more likely that you do not have write permission on the directory. Using rm -f suppresses error messages (and prompts).
One other possibility (probably not the case here), is that the file name has a space or other invisible character at the end, and the name you specify as a file doesn't actually exist (the file is "test2" and not "test2"; or maybe it is "test1<bs>2" where the <bs> represents a backspace, or … there are endless ways to run into problems).
Rerun rm test2; respond to the prompt; look at the error messages.
Or run ls -ld . in the directory containing the file and look at the permissions, but remember that ACLs (access control lists) and extended attributes can make it harder to work out what your permissions are (though again, they're unlikely to be a factor in the problem).
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I have two scripting files to be moved to a new directory.
with mv command I moved those files but couldn't find them in the new directory.
I did a mistake of not providing the filenames in the destination folder. The files are not present anywhere. How to get my files back?
from user1 I moved files
sudo mv script1 /infinitescripts
sudo mv script2 /infinitescripts
I expected script1 and script2 files to be present in infinitescripts directory. But, the directory is empty and the files are not present in the source as well. I dont know where my files are gone.
If I have a file myfile in folder A (so, A/myfile), and I want to move that file into a folder B inside of folder A (so, into A/B/), I need to use mv myfile B. That will result in there being a file A/B/myfile.
What you ran was the equivalent of mv myfile /B, with that extra / in front. What the extra / does is tell the system to look in the root directory for that folder.
So what you did was accidentally created a folder in the root directory called infinitescripts and moved your file into there. The file should be safe and sound. To find it, you can go to that directory with
cd /infinitescripts.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I'm writing a simple archiving script in OSX and I'm trying to use tar to create an archive of a directory deep inside my filesystem. I would love to save that tar on another drive. At some point, I'd love to be able to unpack that tar from the second drive and have it drop right into the same parent directory where I originally archived it. If that directory didn't exist, the tar would create that directory path to that particular folder.
So far I can create a tar that recreates the folder path from the exact place I unpacked it on Drive 2, but I can't figure out how to get it to just drop into a path that already exists. Right now what I'm trying is to get this...
tar cvf Drive2/DirectoryToArchive.tar Drive1/Folder1/ParentFolder/DirectoryToArchive
Followed by this...
tar xvf Drive2/DirectoryToArchive.tar
To yield me an unpacked, reinstated DirectoryToArchive in
Drive1/Folder1/ParentFolder/
I want to be able to give this script to people who don't know terminal and have it work for them without having to place the tar in a specific directory. Is that something tar can do?
Let me show an example of compressing and extracting to the original dir and files using tar.
On host c1669-node2:
% tar -zcvf jdk64.gz /usr/jdk64/jdk1.8.0_112/
% pwd
/root
% ls -1 | grep -Ei jdk
jdk64.gz
On host kyan-hdp01:
% scp root#c1669-node2:/root/jdk64.gz /tmp/
% tar -xzvf /tmp/jdk64.gz -C /
% ls -lh /usr/jdk64/ | head -n 5
total 0
drwxr-xr-x. 8 10 143 255 Sep 23 2016 jdk1.8.0_112
So I think if you compress the file with a absolute path then extract it use the -C / to specify dir it would be exactly as the same path as the orinal dir.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have this directory called "mock", which contains 3 directories. I am trying to copy all the items from "mock" directory into the "projweek" directory using the following command:
cp /mock/* ~/projweek
But I get this error:
cp: cannot stat ‘mock/*’: No such file or directory
Any ideas as to why that is?
If your source directory is set in quotes, then make sure that the * is outside the quotes, i.e.
cp "source/"* dest
or
cp "source"/* dest
It's an odd thing about the unix system that glob expansion (aka use of the "*") is done by the shell, and not by the program you are calling, and furthermore, if the glob doesn't match anything, instead of expanding to nothing, it expands to itself and passes that to the program. So the cp command sees literally "/mock/*" which doesn't exist, because you have no file called "*". Somewhat perversely if you had a file called "*" it would dutifully copy it without complaining.
cannot stat = file/dir does not exist. Check the path first.
And, you say you want to copy /mock but the error message says mock. Show the real code first.
When I test in ubuntu, cp (GNU coreutils) 8.28, I have no problem with copying all files under a dir to another dir, when both paths are correct.
root#DESKTOP-9NHNV2I:~# cp /root/temp/* /root
root#DESKTOP-9NHNV2I:~# ls
temp test.txt test2.txt test3333.txt
cp is used in unix/linux for copy
cp /mock/* ~/projweek this means copy from /mock folder all files to folder projweek that resides in root
This means cp: cannot stat ‘mock/*’: No such file or directory unable to copy all files from mock folder because file or directory not exists on relevant path
cp: cannot stat ‘mock/*’: No such file or director
Check that the files exist on the path.
Also to copy all the files in a folder to another location, use . operator like: cp /source/. /dest/
When I configured shell script on jenkins(See the following lines), I got this error "cp cannot stat ... No such file or directory".
ssh user#remoteNode
cd /somedir
cp fromdir/xxfile todir/xxfile
The following command solves my problem.
ssh user#remoteNode "cd /somedir; cp fromdir/xxfile todir/xxfile"
Why?
Double quotation marks are required. If not, the cp command will be executed locally.
Thanks to CDSN blogger Jinking01.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
The default install directory of apt-get is /opt.
Can I change it to another directory?
Best way I can think of is to use a symbolic link
note that not all programmes are installed to the same directory and /opt may not be the best thing to move. (see end for example of moving only one folder/program)
This is what I did with EasyPeasy (Ubuntu 10.04)
Follow this code carefully some of the commands can delete important files if used incorrectly.
First you need to make sure /opt (or your distros default apt-get install directory) is empty. If you have data in the opt folder, which you most likely do, you can move it to somewhere else first for safe keeping:
sudo mkdir /New_Location/newtmp # Generates Temporary Folder for Programs
sudo cp -a /opt/* /New_Location/newtmp # Moves Programs to Temp folder
Once backed up you can remove the original directory:
sudo rm -rf /opt/ # Removes opt directory
You can then create your new Program Files folder in a drive with lots of space and create a symbolic link:
sudo mkdir /New_Location/Program-Files # Generates New Program Directory
sudo ln -s /New_Location/Program-Files /opt # Creates Symbolic Link
Finally move all your old program files to your new folder and clean up the temporary data:
sudo cp -a /New_Location/newtmp/* /New_Location/Program-Files # Moves Programs to Program Files Folder
sudo rm -rf /New_Location/newtmp/ # Removes Temp folder
If you only wanted to move a single program which is taking up a hunk of your space you could use the same process.
eg:
to move Java (JVM approx 300MB) do the following.
check directory of java using disk usage analyser.
mine is /usr/lib/jvm
sudo mkdir /New_Location/Program-Files/Java # Generates New Program Directory
sudo cp -a /usr/lib/jvm/* /New_Location/Program-Files/Java # Moves Program to new folder
sudo rm -rf /usr/lib/jvm # Removes opt directory
sudo ln -s /New_Location/Program-Files/Java /usr/lib/jvm # Creates Symbolic Link
Its best at this point to do a restart which should clear the cache.
You can't: the installation path is hard-coded in packages (see for example: http://packages.ubuntu.com/oneiric/i386/mono-runtime/filelist). This path is usually /usr instead of /opt, but it depends of the packages. If you want to override the default directory, you must extract manually the content of the packages. But, it can not work: config files, even binary files sometimes, will continue to use the old path. So you must update them in order for the packages to work correctly.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 14 years ago.
Improve this question
I would like to know if it is possible to copy/move files to a destination based on the origin name.
Basically, I have a /mail folder, which has several subfolders such as cur and new etc. I then have an extracted backup in /mail/home/username that is a duplicate. mv -f will not work, as I do not have permission to overwrite the directories, but only the files within.
I get errors such as mv: cannot overwrite directory `/home/username/mail/username.com'
What I want to do is for each file in the directory username.com, move it to the folder of the same name in /mail. There could be any number of folders in place of username.com, with seperate sub sirectories of their own.
What is the best way to do this?
I have to do it this way as due to circumstances I only have access to my host with ftp and bash via php.
edit: clarification
I think I need to clarify what happened. I am on a shared host, and apparently do not have write access to the directories themselves. At least the main ones such as mail and public_html. I made a backup of ~/mail with tar, but when trying to extract it extracted to ~/mail/home/mail etc, as I forgot about the full path. Now, I cannot simply untar because the path is wrong, and I cannot mv -f because I only have write access to files, not directories.
For copying, you should consider using cpio in 'pass' mode (-p):
cd /mail; find . -type f | cpio -pvdmB /home/username/mail
The -v is for verbose; -d creates directories as necessary; -m preserves the modification times on the files; -B means use a larger block size, and may be irrelevant here (it used to make a difference when messing with tape devices). Omitted from this list is the -u flag that does unconditional copying, overwriting pre-existing files in target area. The cd command ensures that the path names are correct; if you just did:
find /mail -type f | cpio -pvdmB /home/username
you would achieve the same result, but only by coincidence - because the sub-directory under /home/username was the same as the absolute pathname of the original. If you needed to do:
find /var/spool/mail -type f | cpio -pvdmB /home/username/mail
then the copied files would be found under /home/username/mail/var/spool/mail, which is unlikely to be what you had in mind.
You can achieve a similar effect with (GNU) tar:
(cd /mail; tar -cf - . ) | (cd /home/username/mail; tar -xf - )
This copies directories, not just files. To do that, you need GNU-only facilities:
(cd /mail; find . -type f | tar -cf - -F - ) | (cd /home/username/mail; tar -xf - )
The first solo dash means 'write to stdout'; the second means 'read from stdin'; the '-F' option means 'read the file names to copy from the named file'.
I'm not entirely clear on what it is that you want to do, but you could try the following:
for file in /mail/*; do
mv -f $file /home/username/mail/$(basename $file)
done
This will move every file and subdirectory in /mail from there into /home/username/mail.
Is using tar an option? You could tar up the directory, and extract it under /mail/ (for I am assuming that is what you want roughly) with tar overwriting existing files and directories.
I'm a bit confused about what it is exactly that you want to do. But you should be able to use the approach of Adam's solution and redirect the errors to a file.
for file in /mail/*; do
mv -f $file /home/username/mail/$(basename $file) 2> /tmp/mailbackup.username.errors
done
DIrectories will not be overwritten and you can check the file so that it only contaions errors you anticipate.
Can you untar it again? The -P option to tar will not strip leading "/", so the absolute pathnames will be respected. From your edit, it sounds like this'll fix it.
Even with your clarification I'm still having a problem understanding exactly what you're doing. However, any chance you can use rsync? The src and dest hosts can be the same host for rsync. As I recall, you can tell rsync to only update files that already exist in the destination area (--existing) and also to ignore directory changes (--omit-dir-times).
Again, I'm not quite understanding your needs here, but rsync is very flexible in backing up files and directories.
Good luck.