Copy Linux files to another location - linux

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.

Related

In my shell script I would like to list all the files in my current directory

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.
So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

Cygwin copy directory with wrong name

I create a little script to copy a directory.
Sadly, cygwin do not copy the name of the directory, but instead copy all the content in a directory called ยท(it's an intepunct, not a dot en.wikipedia.org/wiki/Interpunct ).
There are no problems to execute all the commands directly in the terminal!
Anyone has ideas?
this is the script:
cp -r //REMOTE-PC/folder1/folder2/folder-to-copy/ ./local-folder/folder-to-copy/
thanks

Shell script in cron.daily does not work

I want to backup my database daily automatically, so I made a shell script, and then put it in cron.daily folder in Ubuntu 12.
The script is not complicated,
#!/bin/sh
DIR=`date +%m%d%y`
DEST=/db_backups/$DIR
mkdir $DEST
mongodump -d myapp -o $DEST
this script works well when I run manually like ./automongobackup.sh then It make a backup file in proper location. So I expected If I put it in cron.daily, the backup database will generated automatically, But I checked backup folder today the folder was empty and realize something wrong.
Should I set a another option? The chmod is 755. I attached some screenshots, The first one is my ls-l in cron.daily and second is script. Any missing I did?
Try renaming your script to 'automongobackup' rather than 'automongobackup.sh' as run-parts which handles the crons in cron.daily, and cron.hourly etc doesn't like fullstops/periods in the filename.
Reference: https://askubuntu.com/questions/611336/why-putting-a-script-in-etc-cron-hourly-is-not-working

How do you run bash script as a command?

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.
The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.
Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?
The script needs to be executable, with:
chmod +x scriptname
(or similar).
Also, you want the script to be located in a directory that is in your PATH.
To see your PATH use:
echo $PATH
Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.
You can add a directory to your PATH with:
PATH=$PATH:/name/of/my/directory
and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.
You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)
ln -s /path/to/script /usr/local/bin/scriptname
This should do the trick.
You can write a minimal wrapper in your home directory:
#!/bin/bash
exec /yourpath/yourfile.extension
And run your child script with this command ./NameOfYourScript
update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)
Otherwise, you can use an alias; you will have to amend your .bashrc
alias menu='bash /yourpath/menuScript.sh'
Another way is to run it with:
/bin/bash /path/to/script
Then the file doesn't need to be executable.

simple shell script to copy files and folders and also execute a command

I haven't written any Shell scripts before, but i have to write a simple shell script to do the following;
I will keep all the required files in a single folder and bundle it with this shell script as a tar file; so when the user runs the shell script, it needs to copy the respective files to the respective destinations.
The execution of copy as follows:
copy the plugin.so file to /usrlib/mozilla/plugins/
copy the .so library files to /usr/local/lib/
copy some header files directories(folders) to /usr/local/include/
and finally, need to do ldconfig.
Basically, you can add in a script any command you are able to type inside the terminal itself. Then, you have two options for executing it:
Execute it from the terminal with sh your_script.sh. You don't even need to give execute permission to it with this solution.
Give it the execute permission and run it with ./your_script.sh.
For the second solution, you have to start the file with what is called a shebang. So your script will look like:
#!/bin/sh
cp path/to/source path/to/destination
cp path/to/source path/to/destination
cp path/to/source path/to/destination
ldconfig
echo "Done!"
Nothing else. Just write the commands one after the other.
The first line is the so-called shebang and tells the shell which interpreter to use for the script.
Note: the extension for shell scripts is usually .sh, but you can actually name your file however you prefer. The extension has no meaning at all.
Good scripting!

Resources