Tag Info

New answers tagged

0

Linux Walkthrough of creating a file with dashes and spaces, then removing it. BE CAREFUL! Don't accidentally run a rm -rf / or similar cascade delete command. If your file you are trying to remove includes asterisks or slashes, do not accidentally pump a . or /* or * or some other wildcard which could cascade delete your operating system. Create a ...


0

Xorg --configure while X is not running did it for me - I'm on debian Sid (unstable). You MUST NOT have X running when you do this, and must be in a console TTY. (ctrl-alt-f1/f2/f3/f4/f5/f6) If Xorg.conf doesn't change after doing this, and the program didn't return an error but printed an Xorg.conf configuration file to the screen, do Xorg --configure ...


4

Shells keep track of symbolic links in the path to the current directory (this is known as logical current directory tracking). If you want to expand all symbolic links, pass the option -P to the cd builtin (for physical current directory tracking): cd -P logic If you're in a directory which you've accessed via a symbolic link and want to switch the ...


3

You can use readlink to determine where your link points, and provide this output as the target of your cd. cd "$(readlink <link>)" In the case of additional symlinks pointing to symlinks, readlink will simply provide the target, unless you specify one of it's options to follow symlinks to a canonical file target, for example readlink -f ...


14

With POSIX shell, you can use -P option of cd builtin: cd -P <link> With bash, from man bash: The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command)


1

IFS=. ; set -- ${0+192.168.1.2} printf %b. $4 $3 $2 $1 in-addr.arpa\\c IFS=. ; printf %s\\n \ in-addr.arpa ${0+192.168.1.2} | sed '1!G;$s/\n/./gp;h;d' IFS=. ; printf '[%b.] ' \ ${0+192.168.1.2.]PPPPP\\c} |dc echo in-addr.arpa


2

Try: #!/bin/bash _term() { printf "%s\n" "Caught SIGTERM signal!" kill -TERM $child 2>/dev/null } trap _term 15 echo "Doing some initial work...."; exec /bin/start/main/server --nodaemon & child=$! wait $child Normally, bash will not call trap handler when it's waiting child process. Using exec, your server will be start in a ...


3

grep -oP '[0-9-]{10} [0-9:]{8}' filename


2

This should work only on the GNU version of grep: <file.html grep -oP "(?<=title\=\")\d+-\d+-\d+" Example on regex101 here.


2

With GNU grep, you can do: $ echo 'title="2010-09-11 11:22:45Z"' | grep -oP 'title="\K[^"]+' 2010-09-11 11:22:45Z


13

What's important to understand is that ~ expansion is a feature of the shell (of some shells), it's not a magic character than means your home directory wherever it's used. It is expanded (by the shell, which is an application used to interpret command lines), like $var is expanded to its value under some conditions when used in a shell command line before ...


1

read s <./file [ ${#s} -eq 64 ] && [ -n "${s##*[![:alnum:]]*}" ] && echo success\! If you decide to restrict the test to a-f then change [:alnum:] to [:xdigit:]. Though it's worth mentioning that any application using the POSIX classes [:alnum:] and/or [:xdigit:] will match A-[ZF] as well - it's part of the spec... If that's a problem ...


12

In any version of Bash on any system, yes. ~ as a term on its own is defined to expand to: The value of $HOME so it will always be the same as whatever $HOME is to the current shell. There are several other tilde expansions, such as ~user for user's home directory, but a single unquoted ~ on its own will always expand to "$HOME". Note that the ...


5

Shell scripts are normally treated as if they were the same as any other kind of executable file, such as binaries, Python scripts, Perl scripts, or any other kind of script. They have a shebang at the top that directs the kernel to execute them through the shell. They are expected to be invoked the same way as any other command. As such, a new shell is ...


0

xargs is the unix utility I was looking for. From the man page: The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments. Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from ...


3

How about a whole-line grep grep -qxE '[[:xdigit:]]{64}' myid.id && echo "yes" or (not sure about this one) bash-specific IFS= read -r id < myid.id [[ ${#id} -eq 64 ]] && [[ $id =~ [[:xdigit:]]{64} ]] && echo "yes"


1

Try this out: echo 28fe2baadbe8da32ed0b99c69b11c01b2d141bc5b732b81e0960086de52fc891 | grep [:0-9a-z:] | wc -c


4

Try: $ echo 28fe2baadbe8da32ed0b99c69b11c01b2d141bc5b732b81e0960086de52fc891 | awk '{sub(/\r/,"")} length == 64 && /^[[:xdigit:]]+$/' 28fe2baadbe8da32ed0b99c69b11c01b2d141bc5b732b81e0960086de52fc891 or use perl instead. Include newline: perl -ne 'print if length == 64 and /^[[:xdigit:]]+$/' Exclude newline: perl -nle 'print if length == 64 ...


2

dialog is a great tool for what you are trying to achieve. Here's the example of a simple 3-choices menu: dialog --menu "Choose one:" 10 30 3 \ 1 Red \ 2 Green \ 3 Blue The syntax is the following: dialog --menu <text> <height> <width> <menu-height> [<tag><item>] The selection will be sent to stderr. ...


6

The backticks (``) are command substitution: they are replaced by the result of running the command inside the backticks. Here they run whoami, which prints your username. The - after su makes su run a login shell: a login shell will read certain environment configuration from scratch, among other things. By default it would just run the user's shell as an ...


1

To do it without grep and assume that you don't have duplicated lines, you can: $ sort 20000-words.txt 50000-lines.txt | uniq -u or: $ comm -23 <(sort 50000-lines.txt) <(sort 20000-words.txt)


9

Assuming that 20000-words.txt is already in the format of one word per line, do: grep -vFf 20000-words.txt 50000-lines.txt >50000-filtered-lines.txt The -f argument to grep tells it to read patterns from a file, one pattern per line, instead of taking them as command line arguments. The -F argument to grep tells it that the patterns should be used as ...


0

As an alternative to the other answers, you can use the graphical tool GPRename. It can replace characters, truncate filenames, etc. The advantage is that there's a built-in preview function to check the new filenames before renaming them. But since it works on one directory at a time, it will be inconvenient to use it with numerous folders in sub-folders. ...


0

Here is an awk solution: $ awk -F '/|:' ' $3 == "8013765024" {flag = 1} $0 == ",11:1" && flag {$2 = 2;flag = 0} 1 ' OFS=':' file


0

It is possible for a process to interrogate the file system to determine its current working directory, using a method that’s a little too complicated to be on topic as an answer to this question.  This is what the pwd program and the getcwd library function do.  In the early days of Unix, they were the only ways to find out what your working directory was.  ...


0

With bash: #!/bin/bash while read LINE do if [[ "$LINE" =~ '<' ]]; then echo -e "${LINE/>*</>\\n<}"; fi done < file.html


2

Using an actual html parser isn't that hard: perl -MHTML::Parser -E ' $handler = sub {say "<".shift.">"}; HTML::Parser->new(start_h => [$handler,"tag"], end_h => [$handler,"tag"]) ->parse_file(shift @ARGV) ' file.html <html> <head> <title> </title> </head> <body> </body> ...


1

OP and I worked through this; see comments & chat for details. First, to find the problem process and location, this line in /etc/init/mountall-shell.conf /sbin/sulogin was changed to /usr/bin/ltrace -S -f -o /root/sulogin-ltrace.log /bin/sulogin Excerpt from log: 837 crypt("password", "x") = nil 837 strcmp(nil, "x" <no return ...> 837 --- ...


3

A quick hack with perl: perl -wlne 'print for(/<.*?>/g)' file.html But for a serious solution you should use a tool that really understands html/xml.


2

What is it you are missing? You seem to have understood everything. The > file sends the output to file and 2>&1 sends standard error to standard output. The final result is that both stderr and stdout are sent to file. To illustrate, consider this simple Perl script: #!/usr/bin/env perl print STDERR "Standard Error\n"; print STDOUT "Standard ...


5

You just have to read it left to right: > file --> redirect all thing from stdout to file.(You can imagine you have a link, point-to-point from stdout to file) 2>&1 --> redirect all thing from stderr to stdout, which is now pointed to file. So conclusion: stderr --> stdout --> file You can see a good reference here.


6

With the Perl rename tool (which is called rename on Debian and friends including Ubuntu, it may be prename elsewhere): rename -n 's/(?<!\.)jpg$/.jpg/' * # -n makes it show you what it'll do, # but not actually do it. Remove the -n to # actually rename To break down that patter: the ...


3

While sed is a very useful and versatile tool, you're not using it properly. It's best used to match and substitute strings in text files; it can't directly rename files on the filesystem. This task is better suited for a bash one-liner (assuming that's your shell). To rename something like . filejpg to file.jpg, use this: find . -name '. *' -print0 | ...


1

Though you did start the program in the background by stating &, the output of the background process is still directed to the stdout of your current terminal. That means your prompt is overwritten by something like a echo '\r\n' (overwrites the current line in case your next prompt is completely gone) or echo "Someotherstuff" (prints Someotherstuff ...


4

You're backgrounding the application, and the application is generating output. Your prompt is still there, it just has extra stuff being shown. For example: $ ( sleep 1 && echo hello ) & [1] 24764 $ █ And then after a 1 second delay, I get: $ ( sleep 1 && echo hello ) & [1] 24764 $ hello █ The echo is just writing output to ...


0

I had the same problem on a Synology DS212j, solved using the 3rd part package "config file editor". You can download the package from here . Remember to add /etc/passwd in the "Config file editor" configuration file, (it is the last one in the file list)


3

Who needs a program (other than the shell)? while read a b do echo "$a $b" done < f1.txt If you want the values in the second column to line up, as in polym’s column answer, use printf instead of echo: while read a b do printf '%-2s %s\n' "$a" "$b" done < f1.txt


12

With tr, use the -s ("squeeze repeats") option: $ tr -s " " < file ID Name 1 a 2 b 3 g 6 f Or you can use an awk solution: $ awk '{$2=$2}1' file ID Name 1 a 2 b 3 g 6 f When you change a field in record, awk rebuild $0, takes all field and concat them together, separated by OFS, which is a space by default.


5

Just use column: column -t inputFile Output: ID Name 1 a 2 b 3 g 6 f


1

I would suggest creating a script that runs as root. Have it run hourly, writing the output of 'faillog -a' to a text file everyone has access to. Then have your MOTD grep that file for the current user. This would avoid having to make any unnecessary permissions changes or granting someone sudo access that doesn't need it.


2

Just pipe through a while loop: git diff --name-only develop | grep coffee$ | while IFS= read -r file; do ./node_modules/.bin/coffeelint "$file" done


1

Few comments about your code: If you want to easily insert configuration from "replacements" if it doesn't exist, consider iterating each "replacement" over the lines of the file instead of each line of the file over "replacements". This way, if you don't find any replacement key in the lines, construct and write a new configuration line. I would rather ...


2

I would probably do something like this: # as proposed by csny, only open file quickly (file is closed after with statement) with open('sysctl.conf') as infile: infilelines = infile.readlines() outfile = open('sysctl.conf.new', 'w') replacements = {'Net.ipv4.icmp_echo_ignore_all' :'1', 'Net.ipv4.icmp_echo_ignore_broadcasts' :'1', ...


1

With zsh: print -r -- **/*.csv(D:a:q) Note that some characters (like newline, tab or non-printable ones) are rendered with the $'...' notation which may be a problem for you. Another approach is: print -r -- *.csv(e/'REPLY=${(qq)REPLY:a}'/) Where all the file paths are single-quoted.


0

There is a good answer here in this stackoverflow question For example to extend an alias of 'ls' by adding a '-o' you would do eval "$(alias -p|grep '^alias ls='|sed "s/'$/ -o'/")"


4

For passing file paths as arguments to a command, find does this on its own with its -exec option without any xargs trickery: find /home/user -name '*.csv' -exec yourcommand '{}' + That will find every file called *.csv in /home/user and then execute yourcommand /home/user/a\ b.csv /home/user/my\ dir/c\ d\$2.csv ... with all of the found files as ...


2

If you just installed it it's likely that your shell has cached the old path. Use: hash -r to clear the command hash table and then try running the command again.


5

An ugly way of doing this (i.e. causing a function call in shell based on output from awk) could look like this: awk -F '\t' ' FNR < 2 {next} FNR == NR { for (i=2; i <= NF; i++) { if (($i == 1) || ($i == 4)) printf "retrieve %s\n", $i if (($i == 2) || ($i == 2)) printf "retrieve2 ...


1

A separate printf can do the 0 padding with %0Xd when needed. zero_pad(){ # zero_pad <string> <length> [ ${#1} -lt $2 ] && printf "%0$(($2-${#1}))d" '' printf "%s\n" "$1" } . $ zero_pad "" 5 00000 $ zero_pad "over" 5 0over $ zero_pad "under" 4 under $ zero_pad "exact" 5 exact $ zero_pad " space" 7 0 space


0

Here is a Bash Scripting Tutorial which is good if you are a beginner. If you are new to the Linux command line here is a Linux tutorial which is also good for the beginner.



Top 50 recent answers are included