Cannot remove directory ${env.DELETED_ITEMS} on Linux - linux

I have created a directory on my RedHat machine which has the name ${env.DELETED_ITEMS}. Well, I did not intend to create it, it just got created when i ran my Ant build without setting the correct environment variable. Now, I am unable to remove it. I tried renaming the directory, rm -rf, none of them works. Could anybody explain why this is the behavior and how to get rid of it?
I get this error:
-bash: ${env.DELETED_ITEMS}: bad substitution

Wrap it in single quotes to prevent parameter expansion:
rm -r '${env.DELETED_ITEMS}'
Alternative you will need to escape the special characters:
rm -r \$\{env.DELETED_ITEMS}

You should be able to delete it with the double hypen syntax.
rm -r -- '${env.DELETED_ITEMS}'
This way everything after -- is treated as input.
Or you simple escape those chars. Problem is you are missing the last escape char.
rm \$\{env.DELETED_ITEMS\}

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.

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!

cant delete file using variable name in shell script

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.

Dealing with spaces in directory names in Bash

Disclaimer: I am very new to Bash scripting (and Linux in general), so forgive me for a stupid question.
A friend of mine gave me a script which makes a backup copy of certain files onto Dropbox. Here's the code in full:
#!/bin/sh
DATE=`date +%Y-%m-%d`
tarname='backup-'$DATE'.tar.gz'
cd ~/
directoriesToBack='.bashrc Desktop/School/ Desktop/Research\ Project'
tar -X ~/Desktop/My\ Programs/scripts/crons/exclude.txt -zcvf $tarname $directoriesToBack
mv $tarname ~/Dropbox
The variable directoriesToBack obviously contains the directories to be copied. Exclude.txt is a text file of files which are not to be backed up.
If I try to run this script, I get an error because of Desktop/Research Project: my computer looks for the directory Desktop/Research instead. I've tried to use double quotes instead of single quotes, and to replace \ with an ordinary space, but these tries didn't work. Does anyone know how I can make a backup of a directory with spaces in its name?
Don't try to do this with strings. It will not work and it will cause pain. See I'm trying to put a command in a variable, but the complex cases always fail! for various details and discussion.
Use an array instead.
#!/bin/bash
DATE=$(date +%Y-%m-%d)
tarname=backup-$DATE.tar.gz
cd ~/
directoriesToBack=(.bashrc Desktop/School "Desktop/Research Project")
tar -X ~/Desktop/My\ Programs/scripts/crons/exclude.txt -zcvf "$tarname" "${directoriesToBack[#]}"
I also fixed the quoting of variables/etc. and used $() instead of backticks for the date command execution (as $() can be nested and generally has better semantics and behaviour).
Please run the script and show the EXACT error message. I suspect that what is going wrong is not what you think it is. I suspect that the envar directoriesToBack is not what you think it is.
cd Desktop/"Research Project" (With Quotation marks)
You'll find that a lot of code in many languages use Quotes to signify a space.

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\#\~

Resources