Webmin on Debian 7 Lamp - linux

I accidentally renamed the etc folder, webmin and all services stopped. How I can to rename it back ? Rescue system works but I don't know what commands need to use in terminal..

Renaming (and moving) of files and directories is done via mv. Just pass the old name and the new name as parameters. Example:
mv /foo /etc
Note: If the destination (in this case /etc) already exists, mv will move /foo inside /etc so that it becomes /etc/foo.
In order to see what mv actually does, you can add the -v parameter. Example:
mv -v /foo /etc
This will show you exactly if it renamed and/or moved the given file or directory. In the case of renaming, it will show you something like:
‘/foo’ -> ‘/etc’
and if it moved the directory it will show instead:
‘/foo’ -> ‘/etc/foo’

Related

Renaming a script using Linux mv / rename

I'm trying to rename the script audience_segment_map.sh to audience_segment_map_dedupe.sh using the Linux command line.
I have tried using the mv and the rename commands but they're not having the desired effect:
mv user/local/dmp_job/audience_segment_map.sh user/local/dmp_job/audience_segment_map_deupe.sh
This returns the error
'audience_segment_map.sh' No such file or directory"
but when I use the ls command, the file clearly shows up.
How should I proceed?
Pointing to a path with and without leading / slashes are different (absolute versus relative, respectively). Unless you are in the root directory, most likely you want your command to look like
mv /user/local/dmp_job/audience_segment_map.sh /user/local/dmp_job/audience_segment_map_deupe.sh
where the path is pointing to /user/local/... instead of user/local/...

Git: Recovery of files possible after local remove before push?

I wanted to "clean" my git repo before pushing by removing every JPG file, so I entered:
find . | xargs rm *.png
in the git root and now everything is delted. Also my *.py files are deleted, but I do not know why? It is a Linux, Ubuntu machine. Is there any chance to recover my files? Maybe over my OS?
The command you typed is plain wrong:
find .
This command outputs the name of every file and directory below ., including hidden files.
xargs
This command takes its input and runs the command given as its argument, feeding it one line at a time as an argument. Therefore, it will run rm *.png <line1_from_find>, then rm *.png <line2_from_find>, etc.
There is no safeguard like stop on errors, so if you let the command run completely, it unlinked all files and you know have an empty directory tree. And git will not help you, because it works by storing its metadata and current state within a .git directory at the root of the working directory. In which you just deleted all files.
So no, unless you made a copy, either manually or by pushing you state to some other place, it's probably gone, but see below. For future reference, here is the correct command to destroy all files ending in png:
find . -name '*.png' -delete
Optionnaly add -type f before the -delete if you may have directories ending in .png, to filter them out.
Okay, what now: it happens that git marks some of its internal state as read-only, which rm honors if you didn't use rm -f and you might be able to recover some data from that. Go to the .git directory at your working directory's root. It will contain a objects directory, and some files may have survived there.
Those files are raw compressed streams, you can see their content using that command:
zlib-flate -uncompress <path_to_the_file
(the zlib-flate command comes from qpdf package on ubuntu)
for me the following worked:
$ git checkout -- name_of_deleted_directory
or
$ git checkout -- path_to_local_delected_directory
In my case, I deleted a directory by mistake and I didn't change any code in that directory but I had changed code in other directories of my git repo.

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

How do I mirror a directory with wget without creating parent directories?

I want to mirror a folder via FTP, like this:
wget --mirror --user=x --password=x ftp://ftp.site.com/folder/subfolder/evendeeper
But I do not want to create a directory structure like this:
ftp.site.com -> folder -> subfolder -> evendeeper
I just want:
evendeeper
And anything below it to be the resulting structure. It would also be acceptable for the contents of evendeeper to wind up in the current directory as long as subdirectories are created for subdirectories of evendeeper on the server.
I am aware of the -np option, according to the documentation that just keeps it from following links to parent pages (a non-issue for the binary files I'm mirroring via FTP). I am also aware of the -nd option, but this prevents creating any directory structure at all, even for subdirectories of evendeeper.
I would consider alternatives as long as they are command-line-based, readily available as Ubuntu packages and easily automated like wget.
For a path like: ftp.site.com/a/b/c/d
-nH would download all files to the directory a/b/c/d in the current directory, and -nH --cut-dirs=3 would download all files to the directory d in the current directory.
I had a similar requirement and the following combination seems to be the perfect choice:
In the below example, all the files in http://url/dir1/dir2 (alone) are downloaded to local directory /dest/dir
wget -nd -np -P /dest/dir --recursive http://url/dir1/dir2
Thanks #ffledgling for the hint on "-nd"
For the above example:
wget -nd -np --mirror --user=x --password=x ftp://ftp.site.com/folder/subfolder/evendeeper
Snippets from manual:
-nd
--no-directories
Do not create a hierarchy of directories when retrieving recursively. With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the
filenames will get extensions .n).
-np
--no-parent
Do not ever ascend to the parent directory when retrieving recursively. This is a useful option, since it guarantees that only the files below a certain hierarchy will be downloaded.
-np (no parent) option will probably do what you want, tied in with -L 1 (I think, don't have a wget install before me), which limits the recursion to one level.
EDIT. ok. gah... maybe I should wait until I've had coffee.. There is a --cut or similar option, which allows you to "cut" a specified number of directories from the output path, so for /a/b/c/d, a cut of 2 would force wget to create c/d on your local machine
Instead of using:
-nH --cut-dirs=1
use:
-nH --cut-dirs=100
This will cut more directories and no folders will be created.
Note: 100 = the number of folders to skip creating.
You can change 100 to any number.

Remove a symlink to a directory

I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it.
I tried rm and get back rm: cannot remove 'foo'.
I tried rmdir and got back rmdir: failed to remove 'foo': Directory not empty
I then progressed through rm -f, rm -rf and sudo rm -rf
Then I went to find my back-ups.
Is there a way to get rid of the symlink with out throwing away the baby with the bathwater?
# this works:
rm foo
# versus this, which doesn't:
rm foo/
Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.
At any rate, the first should work, while the second should complain about foo being a directory.
If it doesn't work as above, then check your permissions. You need write permission to the containing directory to remove files.
use the "unlink" command and make sure not to have the / at the end
$ unlink mySymLink
unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
I think this may be problematic if I'm reading it correctly.
If the name referred to a symbolic link the link is removed.
If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.
https://linux.die.net/man/2/unlink
rm should remove the symbolic link.
skrall#skrall-desktop:~$ mkdir bar
skrall#skrall-desktop:~$ ln -s bar foo
skrall#skrall-desktop:~$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar
skrall#skrall-desktop:~$ rm foo
skrall#skrall-desktop:~$ ls -l foo
ls: cannot access foo: No such file or directory
skrall#skrall-desktop:~$ ls -l bar
total 0
skrall#skrall-desktop:~$
Use rm symlinkname but do not include a forward slash at the end (do not use: rm symlinkname/). You will then be asked if you want to remove the symlink, y to answer yes.
Assuming it actually is a symlink,
$ rm -d symlink
It should figure it out, but since it can't we enable the latent code that was intended for another case that no longer exists but happens to do the right thing here.
If rm cannot remove a symlink, perhaps you need to look at the permissions on the directory that contains the symlink. To remove directory entries, you need write permission on the containing directory.
Assuming your setup is something like: ln -s /mnt/bar ~/foo, then you should be able to do a rm foo with no problem. If you can't, make sure you are the owner of the foo and have permission to write/execute the file. Removing foo will not touch bar, unless you do it recursively.
I also had the same problem. So I suggest to try unlink <absolute path>.
For example unlink ~/<USER>/<SOME OTHER DIRECTORY>/foo.
On CentOS, just run rm linkname and it will ask to "remove symbolic link?". Type Y and Enter, the link will be gone and the directory be safe.
I had this problem with MinGW (actually Git Bash) running on a Windows Server. None of the above suggestions seemed to work. In the end a made a copy of the directory in case then deleted the soft link in Windows Explorer then deleted the item in the Recycle Bin. It made noises like it was deleting the files but didn't. Do make a backup though!
you can use unlink in the folder where you have created your symlink
If rm cannot remove a link, perhaps you need to look at the permissions on the directory that contains the link. To remove directory entries, you need write permission on the containing directory.

Resources