Hot answers tagged

31

Your directory looks empty, but the ls output indicates that there is a file in there since the link count for the directory is 3 rather than 2 (an empty directory on an APFS filesystem should have a link count of 2). This implies that your filesystem has managed to get itself into an inconsistent state and that you should probably run fsck on it. On macOS,...


21

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 ...


18

Using an extended globbing pattern in bash: rm ./!(*999*) This requires shopt -s extglob to be enabled (and for safety, also shopt -s failglob, so that no file with the unusual name !(*999*) gets removed by mistake if all filenames contain 999). The pattern !(*999*) will match any name in the current directory, except those names matching *999*. To also ...


16

Just invert the name condition in find : find . -type f \! -name "*999*" Add -delete or -exec rm {} + to actually remove the matched files.


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 ...


9

The directory entries . and .. are an artefact from the early 1970s, when UNIX did run on tiny machines and code needed to be tiny as well. So the people created real hardlinks to directories named . and ... This bad idea causes the hard link count of an empty directory to be 2. Today, this is nonsense and the POSIX definition since 1988 is that these ...


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) ...


8

/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 ...


7

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 ...


7

Sourcing an rc file rarely if ever works in practice, because people rarely write them to be idempotent. A case in point is your own, where you are prepending the same directory to the fpath path every time, which of course means that searching that path takes a little longer each time. No doubt this isn't the only place where you are doing that sort of ...


7

-F field separator in awk. Here we are using 2 field separators. (either { or } ) VBoxManage list vms | awk -F"[{}]" '/Test Machine/{print $2}'


7

awk -F '= ' '/CURRENT_PROJECT_VERSION/{$2=$2+1";"}1' OFS='= ' input > output Tests cat file SOME_DUMMY_VALUE = -1; CURRENT_PROJECT_VERSION = 4; SOME_SECOND_DUMMY_VALUE = -1; CURRENT_PROJECT_VERSION = 4; awk -F '= ' '/CURRENT_PROJECT_VERSION/{$2=$2+1";"}1' OFS='= ' file SOME_DUMMY_VALUE = -1; CURRENT_PROJECT_VERSION = 5; SOME_SECOND_DUMMY_VALUE = -1; ...


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

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 ...


5

Let's start with some "history". /usr/local is typically used to store user programs/data that were not installed with the base operating system. Commonly, when you make programs from source using automake, they will install somewhere under /usr/local. Because the main operating system itself doesn't rely on this directory, permissions are really up to the ...


5

While autoload marks a name for autoloading on first reference in zsh, it doesn't redefine any existing function with the same name should it already be defined, since the name has already been referenced once it was defined. in order to achieve what you desire, you would have to undefine the function first before sourcing .zshrc again or for something more ...


5

My first guess would be that you have files that have been deleted, but are kept open by a running process. Use sudo lsof | grep Contents - this should give you all the PIDs that have files open with names containing the word 'Contents'. Here's the first couple of lines of the output to show you the headers: $ sudo lsof | head COMMAND PID TID ...


5

alias in bash are not designed to take arguments and won't know to what to do with it if provided with one. They should generally be avoided and should only be used for really simple command name alternatives. Recommend using a function instead. Note that pgrep is a valid Linux binary which shouldn't be used, recommend using a non ambiguous name instead. ...


5

The following awk program should work. It looks for ( ... ) elements in each line and checks if they fit the "author(s), year" or "author(s)1 year1, author(s)2 year2, ..." pattern. If so, it creates a citation command and replaces the ( ... ) group; otherwise it leaves the group as it is. #!/usr/bin/awk -f # This small function creates an 'authorYYYY'-...


5

Unix does not have file-name-extensions, nor does Microsoft's Windows (not after Windows ME. However file-explorer still has the concept). What you need to do is find all files starting with ... (in your case starting with cheatsheetold.. You can do this with cheatsheetold.*. It will then pass the file list to rm. You can use it with any command. It is not ...


4

It appears you have to perform a few more steps than just creating the user and settings it's shell. If, like you, I run the following commands: sudo dscl . -create /Users/hideuser sudo dscl . -create /Users/hideuser UserShell /bin/bash And then inspect the users on the system with dscacheutil -q user, I can not see the new hideuser user account and if I ...


4

There are a couple of options to skip searching within the file being created. Create the file in a directory that is not within the tree being searched. Example: grep WindowsIdentity -r > ../windowsid.txt Use the --exclude option. Example: grep WindowsIdentity -r --exclude=windowsid.txt > windowsid.txt


3

The -exec option of find wants pure executables, while alias-es are a shell feature, meaning that they exist only when you are within a shell. You can run any command within a shell by making -exec run bash -c command. However, for aliases to be accepted, the shell has to be run interactively. You can trick the interactiveness by executing bash -ic sha256, ...


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 ...


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

cgroups is a Linux kernel-specific feature, so you won't find it on your Mac.


3

Sparse files may be expanded on copy when the -S flag is not used. (Will make the destination take more space) Hard links within the tree may be expanded to separate files on copy when the -H flag is not used. (Will make the destination take more space) Filesystems may have different allocation sizes. A one-byte file may take up 512 bytes of disk ...


3

Another way. VBoxManage list vms | awk -F ' ' '{print substr($2, 2, length($2) - 2)}' Separate fields by empty space awk -F ' ' Print second column removing the first { and last } characters '{print substr($2, 2, length($2) - 2)}'


3

Another answer, using only awk. This assumes that you adhere to the key <space> = <space> value; syntax throughout: awk '$1 == "CURRENT_PROJECT_VERSION" {$3=($3+1)";"}1' testfile.txt This increases the third field by 1 in all lines starting with CURRENT_PROJECT_NUMBER but otherwise prints all lines "as is" (this is the meaning of the 1 behind ...


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