Recycle bin Script in Linux - linux

In my production server, somebody executed rm -rf and my important files are removed permanently. So, I thought of having a recycle bin, so if a user do rmthe file will move to RecycleBin rather than deleting from server. And i've made the below script for it. But I'm getting some error while it executed.
alias rm='/root/remove.sh'
#rm test_file
Now below script will trigger when you type the rm command
#!/bin/bash
dir=$(pwd)
mv $dir/$1 /root/Recyclebin
when the above script is triggered i'm getting the following error.
mv:cannot move '/root/test_file' to '/root/Recyclebin': Not a directory
Now, please suggest is there anyother way to make a recycle bin concept other than this or please help to resolve the error. Thanks in advance.
I'm using CentOS 5.6

Try This
At first create a folder named as MyTrash under /root ie: /root/MyTrash
Then open .bashrc file and write the below line at the bottom of the file.
alias rm='mv -t /root/MyTrash/'
Here -t means
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
update .bashrc file by running this command source .bashrc
Now if you delete any file using rm command that file will be moved to /root/MyTrash directory

Related

How do I move files out of a broken directory in linux?

I know the premise of the question may be confusing, but I want to understand what happened.
Recently I have been experimenting with the rockchip OK3399 single-chip computer(see here) and have installed a linux system on it with TF card installation. Using Putty and connecting with serial protocol, I was able to establish a connection with the OK3399 computer and control it through my laptop.
I am trying to self-learn some linux with the OK3399 system. I created a bash code by the name of displayvids.sh inside the directory /usr/bin, which is meant to take a variable number of pictures with a mipi camera and then save in a directory for work.
I finished writing the code, but for some reason I cannot run the .sh file when my working directory is not the /usr/bin directory, despite /usr/bin being in the %PATH% environment variable. So, I executed the following command:
mv /usr/bin/display* /usr/local/bin
... attempting to move the file to /usr/local/bin instead. The command ran successfully, but when I tried to run the command:
cd /usr/local/bin
It tells me that I cannot cd to bin
As seen from the above image, the /usr/local/bin is not even a directory. Why would mv succeed if the target was not a directory? How can I retrieve my bash file?
Why would mv succeed if the target was not a directory?
mv can also rename files:
mv foo.txt bar.txt
You renamed your script to bin and moved it under /usr/local.
You may want to remember to add a trailing slash next time, to have mv barf if the target isn't a directory:
mv /usr/bin/display* /usr/local/bin/
How can I retrieve my bash file?
Rename it back.
mv bin displayvids.sh
For future reference, you can use the file command to (try to) identify the contents of a file, if it's installed:
file bin
would have probably said bin: Bash script or similar.

Move files between directories using shell script

I'm new to linux and shell script in general. I'm using a distribution of Debian on the WSL (Windows Subsystem for Linux). I'm trying to write a very simple bash script that will do the following:
create a file in a directory (child-directory-a)
move to the directory it is in
move the file to another directory (child-directory-b)
move to that directory
move the file to the parent directory
This is what I have so far (trying to keep things extremely simple for now)
touch child-directory-a/test.txt
cd child-directory-a
mv child-directory-a/test.txt home/username/child-directory-b
The first two lines work, but I keep getting a 'no such directory exists' error with the last one. The directory exists and that is the correct path (checked with pwd). I have also tried using different paths (i.e. child-directory-b, username/child-directory-b etc.) but to no avail. I can't understand why it's not working.
I've looked around forums/documentation and it seems that these commands should work as they do in the command line, but I can't seem to do the same in the script.
If anyone could explain what I'm missing/not understanding that would be brilliant.
Thank you.
You could create the script like this:
#!/bin/bash
# Store both child directories on variables that can be loaded
# as environment variables.
CHILD_A=${CHILD_A:=/home/username/child-directory-a}
CHILD_B=${CHILD_B:=/home/username/child-directory-b}
# Create both child folders. If they already exist nothing will
# be done, and no error will be emitted.
mkdir -p $CHILD_A
mkdir -p $CHILD_B
# Create a file inside CHILD_A
touch $CHILD_A/test.txt
# Change directory into CHILD_A
cd $CHILD_A
# Move the file to CHILD_B
mv $CHILD_A/test.txt $CHILD_B/test.txt
# Move to CHILD_B
cd $CHILD_B
# Move the file to the parent folder
mv $CHILD_B/test.txt ../test.txt
Take into account the following:
We make sure that all the folders exists and are created.
Use variables to avoid typos, with the ability to load dynamic values from environment variables.
Use absolute paths to simplify the movement between folders.
Use relative paths to move files relatives to where we are.
Another command that might be of use is pwd. It will tell you the directory you are on.
with your second line, you change the current directory to child-directory-a
so, in your third line there is an error because there is no subdirectory child-directory-a into subdirectory child-directory-a
Your third line should be instead :
mv test.txt ../child-directory-b
The point #4 of your script should be:
cd ../child-directory-b
(before that command the current directory is home/username/child-directory-a and after this command it becomes home/username/child-directory-b)
Then the point #5 and final point of your script should be:
mv test.txt ..
NB: you can display the current directory at any line of your script by using the command pwd (print working directory) in your script, it that helps
#!/bin/sh
# Variables
WORKING_DIR="/home/username/example scripts"
FILE_NAME="test file.txt"
DIR_A="${WORKING_DIR}/child-directory-a"
DIR_B="${WORKING_DIR}/child-directory-b"
# create a file in a directory (child-directory-a)
touch "${DIR_A}/${FILE_NAME}"
# move to the directory it is in
cd "${DIR_A}"
# move the file to another directory (child-directory-b)
mv "${FILE_NAME}" "${DIR_B}/"
# move to that directory
cd "${DIR_B}"
# move the file to the parent directory
mv "${FILE_NAME}" ../

Copy Linux files to another location

We have a linux server and for some transactions it is keeping the log files only for the last 10 days. After than the file gets deleted. I want to copy these files to another location using a script. I searched google but couldn't satisfactory result. I'm new to Linux also.
Can someone please guide me if this can be achieved and how ?
You can use the previous answer by nissim abehcera in a sh script:
cp -R SOURCE_DIRECTORY DESTINATION_DIRECTORY
Just paste the bash commands in a text file, name it file.sh and make sure it is executable:
chmod +x file.sh
You can just run the script and it will do whatever you wrote in there.

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.

CentOS 6.7 Root user home deleted?

Some how I managed to delete the /root directory, so I went and recreated and set the proper permissions to the file directory. However, my command line is different and instead of showing root#my-machine# I get -bash-4.1#.
How can I fix this?
Thanks
So you deleted /root and now you have -bash-4.1# in your command line huh?
Well this is caused of a missing / corrupt .bashrc (in your case missing) file in /root (.bashrc sources /etc/bashrc which is what sets the prompt). To fix it, you will run the following command which runs when an account is created. Run as the root user (since it is the user having the problem) or you can define the destination path.
command: (make sure you are in /root)
cp -v /etc/skel/.bash* ~/
Exit terminal and log back in.
I was able to fix the error by creating a new .bashrc
Because the /root directory was removed the default .bashrc was deleted.
I fixed it by running
/bin/cp /etc/skel/.bashrc ~/
and then sourcing the new .bashrc file with source ~/.bashrc

Resources