Tag Info

Hot answers tagged

41

> is the default continuation prompt.That is what you will see if what you entered before had unbalanced quote marks. As an example, type a single quote on the command line followed by a few enter keys: $ ' > > > The continuation prompts will occur until you either (a) complete the command with a closing quote mark or (b) type ...


33

If the first character of file name is printable but neither alphanumeric nor whitespace you can use [[:punct:]] glob operator: $ ls *.txt f1.txt f2.txt ♫abc.txt $ ls [[:punct:]]*.txt ♫abc.txt


17

In Bash, you can use Bash's built in string manipulation. In this case, you can do: > text="some text with spaces" > echo "${text// /}" sometextwithspaces For more on the string manipulation operators, see http://tldp.org/LDP/abs/html/string-manipulation.html However, your original strategy would also work, your syntax is just a bit off: > ...


15

That will happen if you have an unclosed quote in your command. That's something like: $ echo "test here > > ... You can exit that mode by closing the quote (write a " or ', or whatever your open quote is). It could also be a brace-delimited block, a partially-complete for-do or while-do loop, or certain other constructs. You can also press Ctrl-C ...


14

You should never parse /etc/passwd directly. You might be on a system with remote users, in which case they won't be in /etc/passwd. The /etc/passwd file might be somewhere else. Etc. If you need direct access to the user database, use getent. $ getent passwd phemmer phemmer:*:1000:4:phemmer:/home/phemmer:/bin/zsh $ getent passwd phemmer | awk -F: '{ ...


14

The command line arguments of every process in the system is considered "public". Not just the w command, but ps and top and many other commands access that information as a matter of course. Indeed no special privileges are required to get that information. On Linux, you can read the command line of another process, even a process belonging to another user, ...


12

The easiest way to accomplish what you want, is to delete all files in the directory that are older than 30 days. You can use this find command: find /path/to/log/ -maxdepth 1 -name "NameLog.log*" -mtime +30 -delete Explanation: (see: explainshell) -maxdepth 1: don't go deeper into the folder structure -name "NameLog.log*": applies only on log file with ...


9

There are errors in your assumptions, but first some background: You should discern two uses of -exec: with \; the {} will be replaced by a single item found with + the {} will be replaced by many items (as many as the commandline can hold). Therefore your example of -exec use invokes as many cp command as items found by find. Using find ... -exec ...


8

I see another interesting method from here. rand=$(openssl rand 4 | od -DAn) This one also seems to be a good option. It reads 4 bytes from the random device and formats them as unsigned integer between 0 and 2^32-1. rand=$(od -N 4 -t uL -An /dev/urandom | tr -d " ")


7

You don't need echo command at all, just use Here String instead: text=$(tr -d ' ' <<< "$text") Just for curiosity I checked how much time such a trivial task takes for different tools. Here are the results sorted from slowest to fastest: abc="some text with spaces" $ time (for i in {1..1000}; do def=$(echo $abc | tr -d ' '); done) 0.76s user ...


7

In addition to the other answers, you also get the continuation prompt when you type a \ as the last character on a line.


7

I wrote a small script to do this called he, e.g. he apt-get autoclean. The basic strategy is: search for the word (e.g. autoclean) as the first word on a line, then print until the next blank line. You can get something similar using basic sed, e.g. man apt-get | sed -ne '/^ *autoclean/,/^$/p' You can find the script on my github page (linked above) ...


6

The shell waiting for you to complete the command. Maybe there's a unclosed quote somewhere or it thinks you are starting a "for" loop and waits for the user to finish typing


6

The answer lies in this cryptic mention in the Bash Reference Manual: 5.1 Bourne Shell Variables [...] PS1: The primary prompt string. The default value is ‘\s-\v\$’. See Printing a Prompt, for the complete list of escape sequences that are expanded before PS1 is displayed. PS2: The secondary prompt string. The default value is ‘>’. ...


6

ls has some switches (like --quote-name, --escape, --literal) for dealing with unprintable characters, but in this case it seems the character is "printable" but not "typeable" (at least on my keyboard!), so none of these switches seem to help. Therefore, as a general "brute force" approach to get rid of files with any characters in their names, you can do ...


5

Just modify your text variable as below. text=$(echo $text | tr -d ' ') However, if we have control characters this might break. So, as per Kasperd's suggestion, we could have double quotes around it. So, text="$(echo "$text" | tr -d ' ')" will be a better version.


5

If you want a number from 0 through (2^n)-1 where n mod 8 = 0 you can simply get n / 8 bytes from /dev/random. For example, to get the decimal representation of a random int you could: od --read-bytes=4 --address-radix=n --format=u4 /dev/random | awk '{print $1}' If you want to take just n bits you can first take ceiling(n / 8) bytes and right shift to ...


5

No, it's not safe to pass passwords to programs on the commandline. It's better to use: mohsen@debian:~$ mysql -uuser -p Enter password:


5

A similar approach would be to list all files that don't begin with "normal" characters. In bash you can do this with $ shopt -s extglob $ ls !([[:alpha:]]*) However, that does not seem to be available to fish, so you could use find instead: $ find . -type f -not -name '[[:alpha:]]*'


4

$ sed 's. ..g' <<< $text namewithspace


4

Can it be zsh? max=1000 integer rnd=$(( $(( rand48() )) * $max )) You may want to use seed as well with rand48(seed). See man zshmodules and man 3 erand48 for detailed description if interested.


4

The simplest that occurs to me is ls [^a-zA-Z0-9]* and it does the trick for me, but terdon's answer is better in bringing attention to the extglob shell option or even a shell-independent approach.


4

Rename symlinks One approach to handle file names with special characters - as first characters or elsewhere in the filename is to rename to simpler names. This can be used even if you need to keep the original filenames: Rename a copy of the filenames. That can be done by copying the files, but also by creating symlinks or hardlinks to the files, and ...


4

You can't use a terminal to create a file. You can use an application running in a terminal. Just invoke any non-GUI editor (emacs -nw, joe, nano, vi, vim, …). If you meant using the command line, then you are asking how to create a file using the shell. See What is the exact difference between a 'terminal', a 'shell', a 'tty' and a ...


4

Make a function: email_myself() { sudo mail -a "$1" -s "Subject of E-mail" myemail@gmail.com; } If you want your function to hang around permanently, and assuming that bash is your shell, add the definition as a line to your ~/.bashrc. For those who prefer shell scripts to functions, create a file named email_myself, make it executable (chmod +x ...


4

The easiest way to do that kind of thing is to use the shell's globbing and/or brace expansion features: cat linux.{0..3}.txt or cat linux.*.txt As others have explained, cat does not load the whole file into memory, it will just read a few bytes from it, print them to screen and repeat until everything has been read.


3

you are looking for $myuser's home dir ? awk -F: -v user=$myuser '$1==user { print $6 ;}' /etc/passwd you can use awk -F: -v user=$myuser '$1==user { printf "%s/.ssh\n",$6;}' /etc/passwd to get .ssh dir.


3

Your home folder may not be identical on each system. You can check .bashrc or .bash_profile scripts are handle on each server. for the prompt you need to include export PS1="\u@\h \w> " in your bash login script (.bash_profile) You may want to have your home directory on a NFS mount to be able to see similar environment on each system.


3

The bash prompt is configured by the PS1 environment variable. You can get the same prompt as you desire by adding export PS1="[\u@\h \W]\$ " to your .bashrc file (located in your user's home directory). The full list of special characters that you can use for your prompt can be found in the official Bash documentation.


3

I would delete all non-alpha characters using tr and count the number of resultant characters. Passing both the tr solution and your solution to bash's time built-in suggests the tr solution is about 5 times faster, at least on my system tr -cd '[:alpha:]' <filename | wc -m



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