Delete file from Unix system - linux

I want to delete a text file from Unix machine. I am using the following command for the same.
rm /tmp/filename.txt
It is working fine. After execution it returns nothing.
I want to get some conformation about deletion as follows,
filename.txt deleted successfuly.
Please any one help me on this.
Thanks in advance.

You can use these commands:
rm /tmp/filename.txt && echo "File successfuly deleted"
this will remove the file and then (only if the exit status of command is successful) print the message.
Otherwise, as Venkat said, you can use rm -i that asks for confirmation before deleting the file.

I have used the following to slove my issue.
rm -v /tmp/filename.txt
this will display the message as follows,
removed `/tmp/filename.txt`

you can use option with Unix command please See
this link below

The below command prompts you once whether you want to remove the file before deleting it.
rm -i tmp/filename.txt
If the file is unavailable shows you :
cannot remove ‘tmp/filename.txt’: No such file or directory
Hope it helps.

Related

How to change error messaging when using rm in Ubuntu Server (Linux)

I want to be able to change my messages
e.g.:
student#ubuntuserver: rm -v filename
removed 'file'--> I want to change this
to for example You succesfully removed this file!
or something else...
Also when something goes wrong with removing the file it normally says:
No such file or directory!
I want it to say Error when removing file or could not remove file
You can analyze exit code and print custom message depending on success or failure of rm:
rm filename 2>/dev/null && echo "You succesfully removed this file!" || echo "Error when removing file"
The error manages given by rm are part of the rm binary installed on the system.
If you want to change it write a wrapper program that changes the message output when you invoke it.

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)

I can't delete files like "#test.cpp#~" in terminal

I'm using Ubuntu linux. I have a file #test.cpp#~ and when I try to run the following command in the terminal:
rm #test.cpp#~
all I get is:
rm: missing operand
Can someone tell me what's happening? I think the file is an autosave but I don't know how to delete it.
~ and # have special meaning in shell environment. User quotes:
rm "#test.cpp#~"
you need to escape the # try the following:
rm \#test.cpp\#~
you may need to do
rm \#test.cpp\#\~

When I use mv on one of my files, it disappears

When I use
mv Engine_Controls.py ~/Flight_Computer
The file disappears, but I was able to successfully move another python file without an issue into the same directory.
Also, when I type in:
sudo locate Engine_Controls.py
It says that it's in the original directory.. but it isnt.
Could somebody explain why the file is disappearing, as well as why locate says the file is still there?
edit: Problem solved.
Could you have had a typo in your mv command? Try running history | grep mv and see if you might have had a typo.
If you want to fix the 'locate' issue, try a sudo updatedb and then do your sudo locate Engine_controls.py again.
Another option would be to use the -v on the mv command. This will give you some additional info for troubleshooting. For example, if I want to move the file test to test2:
mv -v ~/test ~/test2
the output would be:
'/home/nelsone/test' -> '/home/nelsone/test2'

Adding timestamp to a filename with mv in BASH

Well, I'm a linux newbie, and I'm having an issue with a simple bash script.
I've got a program that adds to a log file while it's running. Over time that log file gets huge. I'd like to create a startup script which will rename and move the log file before each run, effectively creating separate log files for each run of the program. Here's what I've got so far:
pastebin
DATE=$(date +"%Y%m%d%H%M")
mv server.log logs/$DATE.log
echo program
When run, I see this:
: command not found
program
When I cd to the logs directory and run dir, I see this:
201111211437\r.log\r
What's going on? I'm assuming there's some syntax issue I'm missing, but I can't seem to figure it out.
UPDATE: Thanks to shellter's comment below, I've found the problem to be due to the fact that I'm editing the .sh file in Notepad++ in windows, and then sending via ftp to the server, where I run the file via ssh. After running dos2unix on the file, it works.
New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?
mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log
The few lines you posted from your script look okay to me. It's probably something a bit deeper.
You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.
BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.
EDIT
New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?
Two ways:
Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.
A single line method within bash works like this.
[some out put] >$(date "+%Y.%m.%d-%H.%M.%S").ver
will create a file with a timestamp name with ver extension.
A working file listing snap shot to a date stamp file name as follows can show it working.
find . -type f -exec ls -la {} \; | cut -d ' ' -f 6- >$(date "+%Y.%m.%d-%H.%M.%S").ver
Of course
cat somefile.log > $(date "+%Y.%m.%d-%H.%M.%S").ver
or even simpler
ls > $(date "+%Y.%m.%d-%H.%M.%S").ver
I use this command for simple rotate a file:
mv output.log `date +%F`-output.log
In local folder I have 2019-09-25-output.log
Well, it's not a direct answer to your question, but there's a tool in GNU/Linux whose job is to rotate log files on regular basis, keeping old ones zipped up to a certain limit. It's logrotate
You can write your scripts in notepad but just make sure you convert them
using this ->
$ sed -i 's/\r$//' yourscripthere
I use it all they time when I'm working in cygwin and it works. Hope this helps
First, thanks for the answers above! They lead to my solution.
I added this alias to my .bashrc file:
alias now='date +%Y-%m-%d-%H.%M.%S'
Now when I want to put a time stamp on a file such as a build log I can do this:
mvn clean install | tee build-$(now).log
and I get a file name like:
build-2021-02-04-03.12.12.log

Resources