Tag Info

New answers tagged

0

The most important thing is to match the patterns that exist and stick to it. Get to an answer without waste time mulling it over. I understand the irony that I am answering this question ; Apple follows /Library/Extensions/SomeExtension.file so I set up my working directory like that too: Projects/Client/Project/thing.extension Incidentally, the frontend ...


0

You should put your $filename in double quotes: "$filename"


6

ls -A is the correct answer to your question, but not to the question you linked to (that question was about listing only hidden files and directories).


2

To make "playing" with variables a little shorter c=/home/user/working-root-directory/band-folder/album-name/music-file.mp3 for var in file album band do eval "$var=\${c##*/}" c=${c%\/${!var}} done echo $file $album $band music-file.mp3 album-name band-folder Other way is to use read command IFS=/ read -r band album file <<< ...


1

The previous solutions seem overly complicated to me. The following seems to me to be the simpliest solution. To get the directory name of a filename strings in bash dirname "$varcontainingfilename" To get the ONLY the filename from a filename string in bash use basename "$varcontainingfilename" From this base you should be able to easily do what ...


3

When referring to $c if by filename you mean full-path to file then this job is really very easy. There are a few ways to do it. Using just POSIX shell globs and parameter expansion you can do: c='/home/user/working-root-directory/band-folder/album-name/music-file.mp3' file=${c##*/} album=${c#*"${c%/*/"$file"}"/} band=${c#*"${c%/*/"$album"}"/} ...


1

Zsh is a much more elegant solution, but if it is not your shell, you can useawk: printf "%s\n" "$path" /home/user/working-root-directory/band-folder/album-name/music-file.mp3 band=$(awk -F/ '{print $(NF-2)}' <(printf "%s" "$path")) printf "%s\n" "$band" band-folder album=$(awk -F/ '{print $(NF-1)}' <(printf "%s" "$path")) printf "%s\n" "$album" ...


1

I would hire zsh for this job: $ c='/home/user/working-root-directory/band-folder/album-name/filename.mp3' $ echo $c:h /home/user/working-root-directory/band-folder/album-name $ echo $c:h:h /home/user/working-root-directory/band-folder $ echo $c:h:h:h /home/user/working-root-directory (...) $ echo $c:t filename.mp3 $ echo $c:h:t album-name $ echo ...


3

PXELINUX supports the following special pathname conventions: ::filename Suppress the common filename prefix, i.e. passes the string "filename" unmodified to the server. IP address::filename (e.g. node7::filename) Suppress the common filename prefix, and send a request to an alternate TFTP server. Instead of an IP address, a DNS name can ...


0

For only adding numbers into the file. awk 'NR>1{print $1,$2,substr(FILENAME,7),$4 }' xyp/dep* > "xyzp/area1" For sorting by numbers. ls -1v xyp/dep* | xargs awk 'NR>1{print $1,$2,substr(FILENAME,7),$4 }' > "xyzp/area1" For sorting from minus numbers. ls xyp/dep* | sort -t 'p' -k 3 -n | xargs awk 'NR>1{print $1,$2,substr(FILENAME,7),$4 ...


1

You can use sed to convert all / into \ by following command: sed 's/\//\\/g' Example: $ echo $PWD | sed 's/\//\\/g' \home\pandya Another way is to use tr: tr '/' '\\' By above command, tr convertes all / with\; Example: $ echo $PWD | tr '/' '\\' \home\pandya


3

I'm not entirely sure what you're trying to do but changing the slashes is easy: $ printf '%s\n' "${PWD//\//\\}" \home\terdon This is using ksh's string manipulation capabilities also available in bash. Specifically, ${foo//bar/baz/} will replace all occurrences of the string bar with baz in the variable $foo. Since / and \ are special characters, they ...


1

This kind of behavior is configured through styles (except for a few fundamentals that have options). You'll want to turn off path-completion. By default, filename completion examines all components of a path to see if there are completions of that component. For example, /u/b/z can be completed to /usr/bin/zsh. Explicitly setting this style to false ...


1

One of the fundamentals of using regex is that patterns are greedy by nature when specifying the wild card. While the answer proposed by @uloBasEI is certainly a working answer, it also requires the use of the basename command. The original question from @Shixons requests a solution using only sed. Before continuing, it's always helpful to know which ...


1

These appear to be files that Qt creates during the course of Inter-process communication. The file names indicate that shared memory and semaphores were used.


4

In general, the empty string does not denote the current directory, neither to shell commands nor in system calls. It did on some older systems, but not on POSIX-compliant systems. Occasionally you'll find a program which uses the current directory when you pass an empty string and the program expects a directory name. This is sometimes deliberate, and ...


0

If the files are in the current directory you can just use ls: $ ls *.sh If you want the files in subdirectories also and just need the filename you could do something like: $ find . -name '*.h' -exec basename {} \; There are commands that will get the absence of a string as the current directory, but basically because they will get the current ...


12

A long time ago (in 7th edition, 32V, 4.2BSD, 4.3BSD), at the system-call level a zero-length pathname denoted the current working directory (when used for lookup; it was disallowed when trying to create or delete a file or directory). In System III, it was an error to use a zero-length pathname under all circumstances, and the POSIX standard has this to say ...


0

There are various tricks you can use to get find's output without the leading ./: Use find's -printf option and tell it to only print %f. See man find: %f                     File's name with any leading directories removed (only the last element). ...


2

I cannot think of any example where the empty string denotes the current directory .. You may be thinking of invocations like ls, but that is because ls assumes the current directory if no parameter is given, and in fact it won't take the empty string: ulmi@silberfisch:~$ ls "" ls: cannot access : No such file or directory


4

You can use: find * -name "*.h" Note that files in the current directory whose name starts with . will be omitted, and files whose name starts with - will be interpreted as options by find and cause havoc, so this is not a general equivalent to find . …. The absence of of a string ending in "/" as part of a file name implies the current directory, but ...


0

Use zsh and type what comes next. ZSH supports fuzzy auto complete and can deal with it. (Its especially nice with the OH-MY-ZSH plugin.)


5

Use find ... -print0 | while IFS= read -d '' construct: find ${POLLDIR} -type f -mmin +1 -print0 | while IFS= read -r -d '' -r sendfile; do echo "${sendfile}" ls -l "${sendfile}" and-so-on if success_above then mv "${sendfile}" "${donedir}/." fi done The -d '' sets the end of line character to \0 which is what separates each file found by ...


1

I don't use fish, but the documentation says that you can enter a Unicode character by prefixing its hex character code with \u (for 16-bit characters) or \U (for 32-bit characters). I think the code for ♫ is 491eb, so you could do: mv \U000491ebabc.mp3 abc.mp3 to rename ♫abc.mp3. Note that you need the leading zeroes, otherwise abc at the end will be ...


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


0

You didn’t say whether you want to keep these problematic filenames.  One solution might be to “fix” the problem once and for all by renaming (some or all of) your files to names that you can type by running this script: #!/bin/sh for old in * do printf "%s ...? " "$old" if read new && [ "$new" != "" ] then mv -i ...


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


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.


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:]]*'


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


2

Google it. There isn't really a better way. It could be that some other software by the same name exists but no distribution has gotten around to packaging it yet. And even that isn't fully reliable: someone else could be doing the same thing right this minute and conclude that the name is available just as you do. You can check the package lists of major ...


1

If pick outputs of filename per line, you can set IFS to contain a newline only. Eg (in bash): saved_ifs=$IFS IFS=$'\n' for i in $(pick .??*) ; do ... ; done IFS=$saved_ifs This will allow your filenames to contain spaces and tabs, of course if they contain newlines there will still be a problem. You may also want to consider disabling globbing if there ...


0

find -name "* *" -type f -exec rename 's/ /_/g' {} + Replace the spaces in your filenames with "_". Then use pick. Rationale: Spaces in file names are typically considered a non-standard file naming convention. Successful I.T. is about doing things the same way every time, reminiscent of Sun Tzu's art of war: pick your battles carefully.



Top 50 recent answers are included