Hot answers tagged

20

To safely create a temporary directory in the current directory, with a name that is not already taken, you can use mktemp -d like so: tmpdir=$(mktemp -d "$PWD"/tmp.XXXXXXXX) # using ./tmp.XXXXXXXX would work too The mktemp -d command will create a directory at the given path, with the X-es at the end of the pathname replaced by random alphanumeric ...


10

As long as the contents are not sufficient to exceed the maximum parameter limits, (and you don't mind an "acceptable" error message) then it doesn't need to be any more complicated than this: mkdir foo/foo mv foo/* foo/foo Amendment to handle hidden files: mkdir foo/foo mv foo/{.,}* foo/foo


10

On macOS, you can install the rename command (a Perl script) using Homebrew: brew install rename Then using the -p (a la mkdir) to have it make any necessary directories, and -A to add a prefix: % mkdir -p foo/bar; touch foo/{a,b}.txt foo/bar/c.txt % rename -p -A foo/ foo/* % tree foo foo └── foo ├── a.txt ├── b.txt └── bar └── c.txt ...


8

I suggest the other way around. Don't move the directory, but only its content: . └── foo ├── a.txt └── b.txt mkdir foo/foo . └── foo ├── foo ├── a.txt └── b.txt cd foo mv $(ls | grep -v '^foo$') foo cd - . └── foo └── foo ├── a.txt └── b.txt If you have bash, you can also do shopt -s extglob cd foo mv !(foo) ...


6

You've pretty much nailed it already. You could pick a different name for the transient directory, such as the target name with the current date/time in nanoseconds and our PID as a composite suffix, but this still presupposes the directory doesn't already exist: dir=foo # The directory we want to nest now=$(date +'%s_%N') ...


6

/var/folders/qg/s5jp5ffx2p1fxv0hy2l_p3hm0000gn/ This is your Darwin user local directory. Its name is simply a modified base 32 encoding of the concatenation your MacOS User UUID and your MacOS (BSD) user ID. The first two letters of the encoding are used as a "bucket" system to attempt to keep directory sizes low. Those two characters are the encoded ...


6

mkdir foo/foo && mv foo/!(foo) foo/foo You need to cd into the directory where the source folder (foo) is. Then run the command above. It will create a folder called of the same name and move the contents of the parent foo into the child foo directory (except the child directory, hence the ! designation). If the child foo directory already exists ...


6

You may find the shell $OLDPWD shell variable useful (defined by POSIX). For example, you could define a shell function overriding the original cd command: cd() { command cd "$@" && printf '%s -> %s\n' "${OLDPWD}" "${PWD}"; } Excerpt from the Unix man page you should read: If, during the execution of the above steps, the PWD environment ...


3

Using the -path primary: find / -path '*y/x' -path pattern True if the pathname being examined matches pattern. Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``\''). Slashes (``/'') are treated as ...


3

Apple configured ncurses with termcap support (in addition to the default terminfo): config.status file showing the configure options. infocmp calls _nc_read_file_entry to obtain its data. tput calls setupterm, which goes to _nc_read_entry, which calls _nc_read_tic_entry, which does call _nc_read_file_entry if there's a problem in _nc_read_tic_entry, then ...


2

I doubt this is possible with ntpd out of the box: At it's heart ntpd does not keep a "third" clock. (Where the first two are the system clock and monotonic clock). Functionally ntpd is writtent to make adjustments not only to the current time, but also adjust parameters controlling the system clock's run speed. Implicitly it is written to monitor the ...


2

If you know you won't have more than a few thousands of these target directories, you may set the globstar and dotglob shell options in bash with shopt -s globstar dotglob and then use rm -rf ./**/target/ This would delete any directory called target in or below the current directory. The slash at the end is crucial as the pattern may otherwise match ...


2

You have two main issues. First, you are running the script with sh, not bash but are using bash features (the [[ construct) that are not supported by sh. So you need to run bash getos.sh or just ./getos.sh if it is executable. Next, you cannot break commands wherever you like, you need to break on a control operator. So this will fail: SECPKGCOUNT=$(sudo ...


2

Simply don't touch foo and you don't have issues with identical names. Instead, create a directory with the same name inside it and move everything there. Use the $_ trick for that and && to make it a one-liner: cd foo && mkdir $_ && mv * $_ This will throw a harmless error (mv: rename foo to foo/foo: Invalid argument) that you can ...


2

In addition to the suggestions above, you might want to checkout rsync. Before I even start, always remember to use the --dry-run option with rsync before running it without it. The --dry-run will tell you what would happen in real life. rsync has many options that can not only help copy/move files, but also speed up the process. Here is one way to get ...


1

From a zsh shell: print -rC1 -- /**/y/x(/D) The ** glob matches down into subdirectories, and the (/D) glob qualifier specifies that the resulting pathnames must be directories and that the pattern should match hidden names too (as with dotglob in bash). Or the approximate equivalent from bash release 4 or later (i.e., installed from Homebrew on macOS, ...


1

Here you have a mv possibility: for f in *_GWU_*.gz; do mv "$f" "${f/_GWU_/.GWU.}" done


1

You need to change message-style to something more suitable to your status line or terminal background colour.


1

nc on macOS exits with a non-zero exit status if it fails to connect to a host. You can use this fact in an if-statement to output an error message of your own: if ! nc -z -G 5 "$server" "$port"; then printf '%s: no connection (port=%s)\n' "$server" "$port" >&2 fi >>nc-results.log 2>&1 This would add whatever data nc produces on ...


1

ntpd Understanding that you must use ntpd, the only options AFAIK are: disable ntp As seen on the ntp.conf manual page there is the possibility of disabling the ntp feedback loop, or, in layman terms: remove the ability of calculating time corrections between time servers and the local clock. The ntp.conf line needed to activate such option is: disable ...


1

EDIT: based on comments on this question, it is better to fully use the wide functionality of the find command and skip piping altogether (also telling find not to descend into directory it's going to ask rm to delete): find . -type d -name target -prune -exec rm -r {} + Should work fine for you. Sorry all, for posting an unsatisfying answer. Your ...


1

Are more googling and scouring the documentation (mainly terminfo), I finally figured out that I need to fall back to the older termcap code since the capname is not supported for all terminfo capabilities. ed=$(tput ed || tput cd)


1

Both your client and the server are complaining that they can't find the xauth program. The "debug1: No xauth program" message comes from your client, saying it can't find a copy of xauth locally. The "Remote: No xauth program; cannot forward X11" message is from the server, saying it can't find xauth either. The default location for both client and server ...


1

Add /opt/X11/bin to your PATH in ~/.bashrc, e.g. case $PATH in (*"/opt/X11/bin"*) ;; (*) PATH=$PATH:/opt/X11/bin ;; esac at the very start of the file. (Strictly one ought to look for "/opt/X11/bin:"*|*":/opt/X11/bin:"*|*":/opt/X11/bin"|"/opt/X11/bin" but *"/opt/X11/bin"* is almost certainly good enough)


1

I don't think you're doing anything wrong, you just need to tell Terminal.app to use the "Option" key as "Meta" instead. Just go to the preferences for Terminal (⌘+,), then Profiles, then the Keyboard tab for your default profile and check "Use Option as Meta key." Otherwise, you're getting the default macOS behavior (in most keymaps) where Option + key ...


1

Test on FreeBSD 11.2-RELEASE matched the correct results, and matching your results on a recent GNU/Linux. Perhaps the behavior is a more general case of the bug described here: https://github.com/onetrueawk/awk/issues/45 As far as filing a bug, since it seems that the bug per se is already fixed, and the remaining problem is that Apple is so far behind "...


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