How to create a shell script - linux

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

Related

Can't remove a linux directory using linux terminal [duplicate]

This question already has an answer here:
Delete folder that contain subfolders and files on linux
(1 answer)
Closed 1 year ago.
I've been trying to remove an unwanted file: 'PycharmProjects' but I can't seem to be able to do it. Every time I use the rm command (as in rm filename) the linux terminals says this: rm: cannot remove 'PycharmProjects': Is a directory. I've also tried trying to just unistall it from files but every time I do that an error occurs. Could you please help me.
NOTE: I use chromebook
If linux says that is a directory please try to run
rm -d filename
and if not worked try next command
rm -r dirname
here also is an article about deleting files and directories in linux command line:
https://linuxize.com/post/how-to-remove-files-and-directories-using-linux-command-line/
you would try
rm -rf "name_directory"
with rm you will use the remove command; with -r you will select anything on specified directory; and with -f you will force, omiting any rule or barrier of security so you must be careful because a rm -rf command could delete any important data from your disc so you will need admin permissions to execute that command but you will discover could be useful sometimes.
P.D. when you need help with any command you can use the man command that will show you a manual for the command selected for example in this case you can write
man rm
that will show you all the options that you can do with that command depending to the command you also can find information like the developer of the command; common structures and more interesting information.
normally the man command come preinstall in the popular distrubutions but if you type "man" and it isnĀ“t work you could search on internet how to install the man on your linux distribution
have a nice day and welcome to linux :)
Generally, rmdir is the correct way to remove a directory in Linux (and mkdir to create a directory). If your directly is not empty, then rmdir won't remote it.
The command rm -rf <dirname> (where "dirname" is your directory's name) is the less safe way to remove a directory and all of its contents. Only use it if you're sure that the directory doesn't contain information you want to preserve. Remember the rm and rmdir commands don't put anything in a "Recycle Bin" or similar!

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.

Move script.sh to bin

I'm new to shell programming and I'm trying to create a simple script that gives me some infos on the status of the machine (i.e date, time, users logged in etc) on Scientific Linux 6 (I know it's old, but the department of my university runs on it so there's no escaping)
Basically I've created my script "sysinfo.sh"
#!/bin/sh
....
exit 0
as root user I want to move it so that I can be able to execute it anywhere and I thought the right way to do it was
sudo mv sysinfo.sh usr/local/bin
but I get the error message
mv: cannot move `sysinfo.sh' to `usr/local/bin': No such file or directory
then I looked for the PATH and it gives me
$ echo $PATH
/u/geo2/sw//System/tools/bin:/usr/bin:/bin
What is the right place to move my script?
Best practice for these kind of manipulation or learning is to have scripts in your $HOME/bin directory.
mkdir $HOME/bin
export PATH=$PATH:$HOME/bin
mv sysinfo.sh $HOME/bin
chmod +x $HOME/bin/sysinfo.sh
If you anyway want to move it to /usr/local/bin, why not do that with:
sudo mv sysinfo.sh /usr/local/bin
chmod +x /usr/local/bin/sysinfo.sh
chmod command will make the script executable.
from chmod man:
x -- The execute/search bits.
The command that you posted indicates that you were trying to use the absolute path for copying, but you missed a leading slash --
the directory should be /usr instead of usr.
Try
sudo mv sysinfo.sh /usr/local/bin
Note that unless an absolute path is specified, the shell looks for the path relative to the current working directory.
In this case, the shell was looking for the subdirectory usr under the current directory which was not found;
hence the error message.
Thank you very much!
In the end, I didn't realize that the directory /usr/local/bin wasn't in the PATH
So i just needed to
export PATH=$PATH:/usr/local/bin
sudo mv sysinfo.sh /usr/local/bin
:D

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.

Shell Script help to copy directory them remove

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.

Resources