Tag Info

New answers tagged

0

Alternative: cpio (cd /my/big/folder && find . -depth -print0 | cpio -0o > myfolder.cpio) Unpacking to current directory: cpio -id < myfolder.cpio Caveats: If use find /my/big/folder instead of cd, the archive will contain full paths and extraction will try to follow them; Big files (> 2GB) may be a problem;


2

Well, you absolutely can using eval as follows: c=0 for file in $( ls ); do eval "var$c=$file"; c=$((c+1)); done This code will create variables named var1, var2, ... with each one holding the file name. I assume you will have a good reason you want to do that over using an array ...


8

Use tar: tar -cf my_big_folder.tar /my/big/folder Restore the archive with tar -xf my_big_folder.tar -C / -C will change to the root directory to restore your archive since the archive created above contains absolute paths. EDIT: Due to the relatively big size of the archive, it'd be best to send it [directly] to its final location, using SSH or a mount ...


1

Old question, but the cleanest solution for vim in zsh was to add the alias to ~/.zshenv, the file that zsh loads for all shells, login, interactive, or otherwise. This avoids starting vim or zsh with flags and any possible problems with that. There's a nice explanation of ~/.zshenv vs ~/.zshrc here: http://tanguy.ortolo.eu/blog/article25/shrc


1

Possible you mean array. There are some ways to assign values First: c=0 for file in $( ls ); do var[$c]="$file"; c=$(($c+1)); done Second: c=0 for file in $( ls ); do var[c++]="$file"; done Third: for file in $( ls ); do var[${#var}]="$file"; done Fourth: var=( $(ls) ) Fifth ... there is no need to use ls , just put * for ...


5

The answer of Michael is not correct. If the name has a space you are in trouble: $ ls aaa bbb ccc ccc a the last item is "ccc a" $ ls | grep -o -w '\w\{3\}' aaa bbb ccc ccc Celada said the correct answer: $ ls ??? aaa bbb ccc $ ls ????? ccc a


0

This is a start: $ ls | grep -o -w '\w\{3\}' png png log rdb png gfd gpg pub pdf out log gpg key txt txt the com pem VMs $ The count is $ ls | grep -o -w '\w\{3\}' | wc -l 19


2

The command cp -f * /var/www/ copies files matching * in the caller's current directory, i.e. your current directory. It is irrelevant where the script is located.


4

find is an executable not an awk function. S, if you want to call an executable within awk, you have to do that with the system() function. cmd | awk '{system("find " $6 " -xdev -type f -perm -4000 -print")}'


4

The same tools that you can use for other files (generally) can also be used on block devices. This means that you can use, for example, xxd or hexdump to inspect the filesystem: $ sudo xxd /dev/sda2 | head -10 00000000: eb58 9053 5953 4c49 4e55 5800 0200 0000 .X.SYSLINUX..... 00000010: 0000 0000 0000 0000 3f00 ff00 0008 2000 ........?..... . 00000020: ...


1

GNU date accepts a number of relative dates that you can supply with the -d flag. $ date -d '+1year' Sun Apr 17 09:15:14 PDT 2016 See Relative items in date strings for details.


1

If you want to let date create the standard date formatting you could do: $ date -d "$( date -d "Fri Apr 17 20:16:01 IST 2015" "+2016-%m-%d %T" )" But you can also use just one date instance and use the appropriate time format specifiers, as in: date -d "Fri Apr 17 20:16:01 IST 2015" "+%a %b %e %T %Z 2016" Or - since your date format has the year at ...


2

Based on this answer I believe using varname="ENV_$env" echo ${!varname} could be a solution.


0

Try executing the export in the scope of a call to eval: eval export ENV_${env}=some_param This ensures that the shell expands ENV_${env} appropriately to one of your expected names, before trying to assign to it. You'll also need to use eval when trying to retrieve the value stored in your variable: eval echo \$ENV_$env Note the backslash - it ...


3

In most cases, that would be the redirection operator (<): $ tr 'a' 'b' /path/to/file ## fails because `tr` works on streams tr: extra operand ‘file’ Try 'tr --help' for more information. $ tr 'a' 'b' < /path/to/file ## works because the file's contents are passed to tr Both command substitution and the redirection operator are defined by POSIX ...


6

Other answers have covered /bin/pwd vs the shell's builtin pwd. If you want to follow symlinks in the Windows style you mentioned, use cd -P: it will change the PWD variable accordingly. If you want to use -P by default, you can add this line to your .bashrc or .zshrc: set -P Other shells may vary.


0

Here is a post summarizing Unix output streams: http://www.devcodenote.com/2015/04/unix-output-streams.html A snippet from the post: There are 3 standard output streams: STDIN - Standard Input - Writes from an input device to the program STDOUT - Standard Output - Writes program output to screen unless specified otherwise. STDERR - Standard Error Output - ...


1

It is an interesting exercise to write a bash function to remove a directory from a path variable. Here are some functions I use in my .bash* files to append/prepend directories to paths. They have the virtue of removing duplicate entries, if any, and work with any kind of colon separated path variable (PATH, MANPATH, INFOPATH, ...). the remove_from ...


5

Contrast pwd and /bin/pwd. pwd, which is a built-in command in many shells, tells you where your shell thinks you are (and hence treats symlinks "soft links" as if they were real directories. /bin/pwd is an external program that tells you where you really are, if necessary by traversing the filesystem tree up to /. It takes no account of symlinks because ...


9

That is a feature of the shell that remembers how you got to where you are. If you have realpath installed you can do: $ realpath /home/dazz/test/1 And lacking that if you have python: $ python -c "import os; print(os.path.realpath('.'))" /home/dazz/test/1 or readlink (from coreutils): $ readlink -f . /home/dazz/test/1 or /bin/pwd (not the shell ...


1

You need to escape the quotes. Think of them as layers of wrapping, in a game of pass the parcel. Each shell unwraps a 'layer'. So: echo "this ; is a semicolon" But if you wanted to run that via ssh: ssh $user@$host echo "this ; is a semicolon" The ssh would unwrap the first layer of the package - sending: echo this ; is a semicolon Which would ...


0

As many people have suggested screen and tmux I'd like to share a comparison of them: screen vs tmux. They both support basic functionality, but have distinct specific features, so one can't say that one is superior to another in all cases. For example, only tmux supports Window-splitting, while only GNU screen can toggle long line wrapping with (Ctrl+a r). ...


3

There is no way to achieve this within a script - scripts start subshell, which is a standalone environment. There's all sorts of reasons for this, but pretty fundamentally - a script cannot tamper with your environment (including your cwd). The closest you get is creating an alias within the current shell. alias chr="cd /" Either that, or 'source' the ...


6

You're leaving a zombie, trivially, because you didn't wait on your child process. Your shell is immediately exiting because you've set up its STDIN in a nonsensical way. pipe returns a one-way communications channel. You write to pipefd[1] and read it back from pipefd[0]. You did a buch of dup2 calls which lead the shell to attempt to read (STDIN) from the ...


1

You need to catch the SIGCHLD signal, and "reap" the zombie process with a wait() system call. Nearly minimal addition of code to your program to add a signal handler function, and set it as the SIGCHLD handler looks like this: #include <cstdio> #include <fcntl.h> #include <cstring> #include <stdlib.h> #include <cerrno> ...


2

If you are using bash, you can set the nullglob option: nullglob If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves. To illustrate: $ ls test1.dummy test1.jpeg test1.jpg test1.test $ shopt nullglob nullglob off $ for file in ...


0

Make sure your paths on the remote are correct. Most scripts don't live under /home


4

In most terminal emulators, long command lines […] are wrapped to a new line before the user submits the command by pressing Enter. This is not a function of the terminal emulator. It is a function of your shell. Your shell is not a full-screen application, but it is doing cursor addressing. When you are editing a command line in a shell, ...


1

The terminal [emulator] is responsible for this. Moving to the first column of the next line is just what the terminal does after filling the last position on a line. You can easily see that it isn't the shell doing this just by entering cat and typing some long lines. Shells like bash and zsh do in fact track the cursor position and the number of columns ...


1

In principle your solution should work, but it is not robust. If, for example, you have in the greped file a comment (or something else) in the matched line, say, like: SOME_PATH=/some/path # this is some path then the comment will also be part of the name that you expand in path. To check that inspect what's actually in the path, e.g. by printf "'%s'\n" ...


2

Here is an awk way to achieve what you want: awk ' NR==FNR { k[$1] ; next } { for (i=1; i<=NF; i++) if($i in k) $i="" ; gsub(/ +/," ") } 1 ' champs.txt t.txt (The gsub is just for convenience, it compresses sequences of blanks.) For you sample data the result is: * student name age professors departement DPTNUM= 20


0

There are 2 methods i know Use "--" to make rm stop parsing command line options, like this: rm -- --help or you can use / before any symbol you don't want to treat as command like rm /-help though these are repeated.. still wanted to help


0

From chsh manual: When altering a login shell, and not the super-user, the user may not change from a non-standard shell or to a non-standard shell. Non-standard is defined as a shell not found in /etc/shells. So you need either to run chsh as root (sudo chsh -s /usr/local/bin/bash user), either to add /usr/local/bin/bash to /etc/shells (sudo echo ...


1

Bash does not forward signals like SIGTERM to processes it is currently waiting on. If you want to end your script by segueing into your server (allowing it to handle signals and anything else, as if you had started the server directly), you should use exec, which will replace the shell with the process being opened: #!/bin/bash echo "Doing some initial ...


0

Your regex-building command is a little bit wrong; besides, it's very inefficient to rebuild the same pattern on every loop iteration. Although bash has a read command it's generally discouraged to use it for processing large amounts of text, since it's very slow, as well as being a common source of scripting errors. So try to restrict the use of read to ...


1

It is not very clear... grep -owf champs.txt t.txt


0

Check your setting in /etc/timezone. In the question you mentioned you are in "GMT+1", if that is what your timezone is set to, your script will always execute at UTC plus one hour. If you set it to e.g. "Europe/Paris", the time of execution will change with the daylight savings time.


0

This one is a bit simpler to use and set-up, named args, etc. https://github.com/uudruid74/bashTheObjects


1

You'd have to write your own objects for the LINQ stuff, but expanding bash to OOP is pretty easy. Check out https://github.com/uudruid74/bashTheObjects


2

the which command you're using assumes the argument "my_command" is inside your PATH. which searches via your global environment variable PATH for the argument that you type and lists the complete path to the first match it finds. You can find your path by typing env on the command line and looking for PATH=. whereis - searches for files in a restricted ...


0

I suggest an approach that is halfway between glenn's two suggestions: use a (simple, scalar) variable, but break its definition into multiple lines: myword="super" myword="${myword}cali" myword="${myword}fragil" myword="${myword}istic" myword="${myword}expi" myword="${myword}ali" myword="${myword}docious"


1

Well, you could simplify it slightly to: $ [ -f /dev/null ] || sed 's/a/A/g' <(echo "thisis""avery"\ "long""string"\ "Ihave""separated"\ "into""smaller"\ "substrings") thisisAverylongstringIhAvesepArAtedintosmAllersubstrings The important point is to not have any extra whitespace in the input string, so no spaces around the \. Glenn's suggestion to ...


0

This may work for you: #!/bin/bash # cd /a || exit find . -type f | while read FILE do TARGET="/app/$FILE" test -f "$TARGET" && cp -fp "$TARGET" "$TARGET.bak" mv "$FILE" "$TARGET" done


0

You can try this: #!/usr/bin/env ksh a_files=( $(find /path/to/a/ -type f) ) for file in "${a_files[@]}"; do mv "/path/to/app${file##*a}" "/path/to/app${file##*a}.bak" mv "$file" "/path/to/app${file##*a}" done If a/ and app/ are in same directory e.g. /home/user/{a,app}, then: #!/usr/bin/env ksh a_files=( $(find /home/user/a/ -type f) ) ...


2

Move your cronjob to 8:00 and sleep one hour if you are in GMT+1 0 8 * * * [ "$(date +\%z)" = "+0100" ] && sleep 3600; place_your_command_here


14

Normally xargs will put several arguments on one command line. To limit it to one argument at a time, use the -n option: $ seq 3 | xargs -n 1 echo 1 2 3 Documentation From man xargs: -n max-args Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s ...


5

A bit more research revealed the answer from Make xargs execute the command once for each line of input: $ seq 1 3 | xargs -L 1 echo 1 2 3


1

From the manual page: The daemon will use, if present, the definition from /etc/timezone for the timezone. The environment can be redefined in user's crontab definitions but cron will only handle tasks in a single time‐ zone.


5

This will likely depend on your OS and it's implementation of cron. This is not possible in the most popular cron implementation, vixie/isc cron. From the crontab(5) manpage: LIMITATIONS The cron daemon runs with a defined timezone. It currently does not support per-user timezones. All the tasks: system's and user's will be run ...


1

python -c "import platform;print(platform.platform())"



Top 50 recent answers are included