I want to delete a file whose name is stored in a variable, but it doesn't work. I'm getting
A file or Directory in the path name does not exist
My code is
value=$(<try_text.txt)
rm -f /home/inform/output/$value
when i tried deleting i got :
cannot remove `/home/oracle/Omar2/B2BFiles/bm.txt\r': No such file or directory
where does the \r come from ?
It comes from an editor which wrote the Windows line ending \r\n to try_text.txt. When reading that file, the Linux shell removed the Unix line ending \n, and the \r remained. To get rid of it, see e. g. the answers to the question Line ending issue DOS > Linux > Java.
Try this:
value=try_text.txt
rm -f /home/inform/output/$value
Don't run the value variable in a subshell when it's not needed.
EDIT
Previously misunderstood the question, didn't see the '<'.
This works on my system:
value=$(</home/user/Documents/try_text.txt)
rm -f /home/user/Documents/$value
as #gile said, make sure the try_text.txt is in your working directory.
Related
I have a shell script located at "/home/pi/scripts/take-snapshot.sh" but when ever I try to execute it I get a error that the file is not present.
the following commands do not work (assuming in script directory):
/home/pi/scripts/take-snapshot.sh
./take-snapshot.sh
take-snapshot.sh
bash /home/pi/scripts/take-snapshot.sh
the following do work and will bring up the shell file (not a new file):
vi take-snapshot.sh
nano take-snapshot.sh
The most likely cause is that your file is not executable. Bash is a bit confusing in that it reports the file as "not found", even though you only don't have permissions to execute it. Run ls -l and check the permissions. The leftmost column should show an "x" at least for the current user. It will usually look something like -rwxr-xr-x for a file you have created yourself.
Run chmod +x take-snapshot.sh to fix the permissions if they don't match.
I have seen this error when the line endings are windows EOL characters. It doesn't give any other error, just the above "No file or directory".
Check the EOL character and convert it to linux EOL, if it is windows and try to run the script again.
By accident I named my image file wrong so I wanted to delete it. So I typed the following in my linux console:
rm localapps\logo.jpg
But it didn't work.
Then I wrote
rm *.jpg
then it worked. Simple question. Why did the first not work , even I know that is the way to delete files in linux?
We would need the output of the commands you are running. You typically have no output when the command succeeds.
It is also important for you to notice that in linux, the / character is used to denote directories, and not \, which is actually typically the escape character.
In a terminal is also very important for you to notice in which directory are you working and what is the relative path to the file you want to refer to. You can find this out with the command pwd that stands for print working directory.
You would see something like
your-box:~ blurry$ pwd
/home/blurry
your-box:~ blurry$
This said, when you type
rm localapps\logo.jpg
since \ is a escape character, this is interpreted as
rm localappslogo.jpg
this means, it is looking for the file named localappslogo.jpg in the current directory (/home/blurry/localappslogo.jpg).
I assume that file does not exist, then, it will output something like:
rm: localappslogo.jpg: No such file or directory
when you type
rm *.jpg
this code removes any file ending in .jpg in the current directory. So notice that if you were trying to delete a file that was in the localapps folder, you should use instead
rm localapps/logo.jpg
But this is always assuming that the relative path to your image is localapps/logo.jpg.
You can also change directory then delete the file like this,
cd localapps
rm logo.jpg
I'm not great in the terminal, and I can't figure out why its returning this. It's probably really obvious so apologies for asking, but the executable file I'm referencing is definitely in that file path, and after researching I can't seem to find an answer:
/home/user/protoc-3.5.1-linux-x86_64/bin/protoc object_detection/protos /*.proto --python_out=.
object_detection/protos/*.proto: No such file or directory
(I can't cd into it as I need to do this in a particular directory)
Thanks
It seems bash is looking for a specific file that has the name "[star]" instead of using this as a wildcard.
I think you might need to use a pipe to get the desired result.
From your command line, it looks like protoc is the executable, located at /home/user/protoc-3.5.1-linux-x86_64/bin/protoc. And that you're giving it two arguments separated by a space: object_detection/protos and /*.proto. If you have spaces in the file path, you'll need to escape them or double-quote them:
protoc object_detection/protos\ /*.proto or
protoc "object_detection/protos /*.proto"
The odd thing is that the error message indicates differently:
object_detection/protos/*.proto: No such file or directory
Or possibly the protoc executable needs the absolute (complete) path for file arguments. If from your current working directory the command ls object_detection/protos/*.proto shows results for you, then you can try running your command like this to use absolute file paths:
/home/user/protoc-3.5.1-linux-x86_64/bin/protoc $PWD/object_detection/protos/*.proto
$PWD is an environment variable that contains your working directory path.
I want to copy a file from a directory using shell script
Suppose I save the directory and file name seperately as
dir=/home/user/directory/
file=file_1
to copy the file Im using this command in my script
cp $dir$file .
But I get this error
/bin/cp omitting directory '/home/user/directory'
I have tried all combination eg. omitted the trail backslah from variable dir, etc but nothings working. I cant understand what is wrong with this code. Pleas help
Maybe the command $dir$file is not getting unpacked in the shell (ie only the directory variable is getting unpacked, not the file variable)!!!!!
It looks like you are having problem with expansion in cp $dir$file . In order to prevent possible problems, it is better to protect your variable with braces and double quote the full path/file to make sure you don't get caught by spaces in either the filename or heaven forbid the user's dirname:
cp "${dir}${file}" .
This will prevent the possibility the second $ is missed. Also make sure you have read access to other users /home (if you are root or using sudo you should be fine)
If you see this, when you somehow assign an empty string to file somewhere. Search your script for file= and unset file.
You can also debug this by adding
echo ".${file}."
in the line before the cp command. I'm pretty sure it prints .., i.e. the variable is empty or doesn't exist.
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