Tag Info

Hot answers tagged

24

You can just use standard globbing on the rm command: rm -- *\ * This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won't be interpreted as arguments by rm). If you want to confirm each file before it's ...


16

It is a bad idea (to have strange characters in file names) but you could do mv somefile.txt "foo bar" (you could also have done mv somefile.txt "$(printf "foo\nbar")" or mv somefile.txt foo$'\n'bar, etc... details are specific to your shell. I'm using zsh) Read more about globbing, e.g. glob(7). Details could be shell-specific. But understand that ...


12

If you use bash, this command should work. mv a $'b\nc'


10

Use single quotes for the expression you used: sed 's/\//\\\//g' In double quotes, \ has a special meaning, so you have to backslash it: sed "s/\//\\\\\//g" But it's cleaner to change the delimiter: sed 's=/=\\/=g' sed "s=/=\\\/=g"


7

FILEPATH_WITH_GLOB="/home/user/file_*" Now, FILEPATH_WITH_GLOB contains /home/user/file_* FILENAME=$(basename "$FILEPATH_WITH_GLOB") FILENAME contains file_*. echo $FILENAME #file_1234 $FILENAME being unquoted in list context, that expansion undergoes the split+glob operator, so that's expanded to the list of matching file: ...


6

For arbitrary content, you can also use: cat << 'EOF' > file RZW"a`4$k6[)b!^"'%*X6Evf RZW"a`4$k6[)b!^"'%*X6Evf EOF (as long as that content doesn't contain lines that consist of exactly EOF, in which case you can use a different delimiter). To include NUL characters (^@ usually entered with Ctrl+VCtrl+Space or Ctrl+VCtrl+@), the above will only ...


6

I would avoid parsing ls output Why not : find . -regex '.* .*' -delete No problem with rm :-)


6

I know you asked for a mv solution, however, despite the warning, this can be easily done with rename (in the Perl package): ~/tmp$ touch foo ~/tmp$ rename 's/$/\nbar/' foo Unsuccessful stat on filename containing newline at /usr/share/perl5/File/Rename.pm line 69. ~/tmp$ ls foo?bar


5

Look at this Suppose name "strange file" Solution one rm strange\ file solution two rm "strange file" solution three ls -i "strange file" you see the inode then find . -inum "numberoofinode" -exec rm {} \; In case of very strange file names like !-filename or --filename use rm ./'!-filename'


5

Just put it in single quotes: echo 'RZW"a4k6[)b!^"%*X6Evf' > file But if you have any single quotes in the string you need to escape each of those in double quotes ("'") and "glue" the result together like this: echo 'text without single quotes'"'"'and other text without single quote'


4

Do an experiment! Run echo grep .* newfile What can you conclude from the result? How does the result change when you quote (place in single or double quotes) the first argument to grep? If you want the straight dope on this, read the POSIX spec for Pathname Expansion. Knowing everything about the various expansions will turn you into a shell guru in no ...


4

One aspect of this problem isn't really about awk - and only a little bit about the shell. The problem is that on a standard, canonical tty most of the time the kernel's tty discipline is buffering your input - just echoing it to your screen and nowhere else - so that it can efficiently handle backspacing and such-like. However, when you press return or ...


4

Lets start with two simple cases: attempt 1: PS1="\$" This does not work for root. Why? The double quote and the backslash. You need one of the next two lines. PS1='\$' PS1="\\$" OK, so that fixed the the dollar sign issue. Now lets look at the fun with alignment and color: Attempt 2, set the color to default: PS1='\033[0m\$' You will notice that ...


3

for i in `cat servlist`;do echo $i;ssh $i 'sudo usermod -c "New Comment" user';done or for i in `cat servlist`;do echo $i;ssh $i "sudo usermod -c \"New Comment\" user";done


3

Try: sed 's/\//\\\//g' or using another delimiter to prevent you from escaping slash: sed 's,/,\\/,g'


3

This matches ( and ) literally. () is used as capturing groups in Extended Regular Expressions (as used by r's grep() or the grep command with the -E option) or Perl Compatible Regular Expression which extend Extended Regular Expressions (as used by r's grep(..., perl=TRUE) or some grep commands with the -P option). Hence to match () literally, \ is used.


3

That is because the process does not exist. The $$ is being evaluated locally, and all servers are being passed the same number. A number that is not a currently used PID on the servers. All the $ stuff is done by the shell, not the commands. You need to escape it, so that it is evaluated by the shell on the server. Try \$\$ e.g. for i in $(cat ...


3

You don't need to escape the quotes inside a subshell, since the current shell doesn't interpret them (it doesn't interpret anything from $( to ), actually), and the subshell doesn't know about any quotes that are above. Quoting a subshell at variable assignment is unnecessary too, for more info see man bash.


3

eval might help this case... ~ $ $(echo expr 1 \\* 2) + echo expr 1 \* 2 + expr 1 \* 2 expr: syntax error ~ $ eval $(echo expr 1 \\* 2) + echo expr 1 \* 2 + eval expr 1 \* 2 + expr 1 * 2 2 But it might be better to look up /proc/$pid/stat on Linux. pid=1155 hz=$(getconf CLK_TCK) uptime=$(awk '{print $1}' < /proc/uptime) starttime=$(awk '{print $22}' ...


3

When you deal with printing variable content, you should stick with printf instead of echo: printf 'visit:%s\n' "$site" will output visit: followed by content of $site and a newline regardless of characters in $site.


3

You can't expand variables in single quotes. You can end single quotes and start double quotes, though: echo 'visit:"'"$site"'"' Or, you can backslash double quotes inside of double quotes: echo "visit:\"$site\""


3

It seems that you have made a mistake when editing PATH variable. Backslash character in your PATH output was considered literal, not escaping for space. You need: PATH="/Applications/Racket v6.2/bin:$PATH"; export PATH or: PATH=/Applications/Racket\ v6.2/bin:$PATH; export PATH


3

The $PATH is getting expanded prior to running on the remote server. Example #1 Say I run these commands from a system called skinner.bubba.net. [root@skinner ~]# ssh mulder 'bash -s' <<EOL > echo $HOSTNAME > hostname > EOL skinner.bubba.net mulder.bubba.net By moving the single quote so that the echo $HOSTNAME is inside it, you can ...


2

Ooooh I found it, I just need to use the $'' syntax instead of $"": $ grep -C1 --group-separator=$'\n\n' 'hello' a hello this is me something else hello hello bye From man bash: QUOTING Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI ...


2

Just for completeness, you dont need all those (") nor the final $(echo ...). Here's the simplified version of your assignments that produce the same effect: STARTIME=$(date +"%T") ENDTIME="$STARTIME today + 10 seconds" CALL="date -d '$ENDTIME' +'%H:%M:%S'" Note how you dont need to quote when doing var=$(...) but you do usually with var="many words": ...


2

Thanks Jeff Schaller for correcting my syntax error. I found a solution to my problem, this is working in Solaris 10. script: #!/bin/ksh HEX=30 DEC=`printf "%d\n" 0x${HEX}` ##Converted Hex to decimal echo "$DEC" OCT=$(printf '%o' $DEC) ##Converted decimal to octal echo "$OCT" ASCI=$(printf \\$OCT) ##Finally converted OCTAL to ASCII. echo "$ASCI" ...


2

You're mistakenly escaping the $ twice, leading printf to see printf \$( ... instead of (what I assume you want) of substituting the inside printf results. To that end, you could simplify that whole statement to: ASC=$(printf '%03o' $DEC)


2

man itself is a tool to format manual pages, not to browse them. The tool you use to browse the man pages is a pager. The default pager on most systems is less. In less, when you press / or ? to search, a few characters have a special meaning if you type them at the beginning of the search expression. The exclamation mark is one of them. To avoid ! having a ...


1

man -k \! Not sure that is what you are really asking?


1

SSH executes the remote command in a shell. It passes a string to the remote shell, not a list of arguments. The arguments that you pass to the ssh commands are concatenated with spaces in between. The arguments to ssh are sudo, usermod, -c, New Comment and user, so the remote shell sees the command sudo usermod -c New Comment user usermod parses Comment ...



Only top voted, non community-wiki answers of a minimum length are eligible