Shell Script help to copy directory them remove - linux

I want to write simple script to copy/backup directory then remove on server startup. So something like this:
TC_DIR=${SERVER_HOME}/terracotta
CLUSTER_STAT_DIR=${TC_DIR}/cluster-stat
cp ${CLUSTER_STAT_DIR} ${TC_DIR}/old.cluster-stat
rm ${CLUSTER_STAT_DIR}
Thanks for help guys.

I believe what you have done should work with the only addition that you need to pass -r options to both cp and rm as you are dealing with directories. Try this:
TC_DIR=${SERVER_HOME}/terracotta
CLUSTER_STAT_DIR=${TC_DIR}/cluster-stat
cp -r ${CLUSTER_STAT_DIR} ${TC_DIR}/old.cluster-stat
rm -rf ${CLUSTER_STAT_DIR}
EDIT: if your question is how to execute that on startup take a look here.

Related

Is there a way to remove file with substitutions in name in linux?

Let's say I have directory with files: log[1-3], log1, log2, log3. I want to type a command and remove file log[1-3]. How it can be done?.
Yes I know, that I can type rm -i log* and then choose file that I wanted to remove, but it is not appropriate for me, because in this directory can be many files begins with log.
So, i need a way to do this with just one command.
I found way to do this. You just need to type rm 'log[1-3]'. With a single quotes
Thank you CamilCuk
There seem to be two ways to do this:
rm log\[1-3\]
rm 'log[1-3]'
rm log\[1-3\] is a result of rm log ESCESC
rm 'log[1-3]' is a result of ls -ltra.

How to create a shell script

I am trying to create a shell script to remove certain files from a directory. How would I be able to achieve this?
Can I write the standard commands in a script as follows:
#!/bin/sh
rm -f /directory/of/file/file1.txt
rm -f /directory/of/file/file2.txt
rm -f /directory/of/file/file3.txt
rm -f /directory/of/file/file4.txt
Or is there a specific way to delete files in a shell script.
This is my first question here, so please bear with me as I do not know all the rules.
Thanks in advance :)
Edit:
Thanks for all the answers in a short matter of time, I really appreciate it.
Forgot to mention this will executed by root cron (crontab -e) every Tuesday and Friday # 5PM.
Do I still need to chmod +x the file if root is executing the file?
Your question can split into a few points:
You can use those commands to delete the specific files (if you have the permissions)
Make sure you add running permissions to the shell script file (that is used to perform the rm commands) by using: chmod +x file_name.sh
In order to delete the folder contents and not the folder itself the command should be: rm -r /path/to/dir/*
Yes you can. However if you don't have the permission to delete the files then you may get error on the statement. Try to handle that error and you are good to go

linux: how to remove file with this kind of name "test\download\"?

I was trying to remove this file from server using rm command but it doesn't work. Also I can't rename this file. Please help.
Try something like
rm -i test?download*
and read glob(7) and rm(1)

force overwrite the copy command ssh?

I'm looking for something like forcing/bypassing overwrite in ssh on copy command??
For Example : If I have a file test1 in /home and in /root
I want to copy the file from root to overwrite on home directory like if I write
cp test1 /home
cp: overwrite `/home/test1'?
How can we remove this question?
How can we force it not to show this overwrite line ... bypass this to yes bydefault
I tried -Rf but not working still I'm seeing this....
Anything on this is great help ...
I check stack overflow and received this answer but didn't get weather it is working or not
yes | cp -R test1 /home
Is there anything wrong on this??
Thanks & Regards
Kishan Giri
you can use cp -f test1 /home command

why can't delete the file?

I am the root user of the system there is a file:
D:\XAMPP\htdocs\magento_41\magento\
which is under htdocs directory.
when in [root#localhost htdocs] i use rm -rf D:\XAMPP\htdocs\magento_41\magento\. it can't remove this file. how to delete it? thank you.
Try
rm 'D:\XAMPP\htdocs\magento_41\magento\'
backslashes are special in the shell (escaping)
Probably your path is wrong. When you're in htdocs dir, try using rm -rf magento_41/magento. Linux doesn't know windows' paths/drive names by default.

Resources