0

I need the equivalent of PHP function is_link() in Javascript.

I have a directory with a mix of files and symlinks:

lrwxrwxrwx 1 www-data www-data   30 Aug 31 11:14 AD164_RNA_1.fq.gz -> ../../shared/AD164_RNA_1.fq.gz
lrwxrwxrwx 1 www-data www-data   30 Aug 31 11:14 AD164_RNA_2.fq.gz -> ../../shared/AD164_RNA_2.fq.gz
lrwxrwxrwx 1 www-data www-data   33 Aug 31 11:14 CD34_2_1.clean.fq.gz -> ../../shared/CD34_2_1.clean.fq.gz
lrwxrwxrwx 1 www-data www-data   29 Aug 31 11:14 HL60_RNA_1.fq.gz -> ../../shared/HL60_RNA_1.fq.gz
lrwxrwxrwx 1 www-data www-data   29 Aug 31 11:14 HL60_RNA_2.fq.gz -> ../../shared/HL60_RNA_2.fq.gz
-rw-r--r-- 1 www-data www-data 7.4G Aug 10 16:19 NT07_NT_DNA_1.fq.gz
-rw-r--r-- 1 www-data www-data 7.6G Aug 10 16:18 NT07_NT_DNA_2.fq.gz
-rw-r--r-- 1 www-data www-data 7.0G Aug 10 16:18 NT07_TP_DNA_1.fq.gz
-rw-r--r-- 1 www-data www-data 7.2G Aug 10 16:20 NT07_TP_DNA_2.fq.gz
-rw-r--r-- 1 www-data www-data 284M Aug 23 14:17 RHH3901_1.fq.gz
-rw-r--r-- 1 www-data www-data 276M Aug 31 10:27 RHH3902_1.fq.gz
-rw-r--r-- 1 www-data www-data  99M Aug 31 10:27 RHH3903_1.fq.gz
-rw-r--r-- 1 www-data www-data  88M Aug 31 10:58 RHH3903_1.trimmed.fq.gz

How can I check if the url to the files in this directory is a real file or symlink something like:

if ('mydir/NT07_TP_DNA_2.fq.gz' is file) {
    // do file stuff
} else if ('mydir/NT07_TP_DNA_2.fq.gz' is symlink) {
    // do symlink stuff
} else {
    // do other stuff
}

using Javascript/JQuery?

Ömer An
  • 600
  • 5
  • 16
  • in a browser? or is this a nodejs question? – Jaromanda X Aug 31 '16 at 03:39
  • in node you mean, surely – Jules G.M. Aug 31 '16 at 03:39
  • in browser, not in node.js – Ömer An Aug 31 '16 at 03:41
  • a URL could be either real or symlink or neither of those, there is no requirement for a URL to map to a file or link entry in a directory somewhere ... in short, your browser can not access the servers filesystem directly ... or put yet another way - if you want your browser to know what type of file a URL "resolves" to, then you need server side code, like PHP, to do the hard work for you – Jaromanda X Aug 31 '16 at 03:41
  • @JaromandaX: if it's possible to check if file exists with some trick like [here](http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascript), it should be also possible to check if it's symlink. – Ömer An Aug 31 '16 at 03:55
  • This trick is not about `file_exists`, it's just `http resource exists` or `http response status is 200`. HTTP protocol has nothing to do with filesystem. It has request and response. Your http server can to add aux headers to answer with filestat info, but it is not default behavior. – vp_arth Aug 31 '16 at 03:59
  • @ÖmerAn - you need to understand what I wrote before going any further - that code you linked to checks for a URL - you specifically stated you want to know the type of filesystem object a URL points to - which is completely different – Jaromanda X Aug 31 '16 at 04:00
  • @ÖmerAn See [How to check if symlink exists](http://stackoverflow.com/questions/5767062/how-to-check-if-symlink-exists), [Symlink check - Linux Bash Script](http://stackoverflow.com/questions/21676882/symlink-check-linux-bash-script) – guest271314 Aug 31 '16 at 04:40

1 Answers1

0

At terminal you can use cat

$ file="filename.ext"
$ cat $file

where, if $file is not a link should print the contents of file to stdout, else print properties of file, for example Encoding, Name, URL, and Type which should print

TYPE=Link

if file is a link.

See also How to check if symlink exists, Symlink check - Linux Bash Script

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • I need Javascript/JQuery solution, not bash. Moreover, your answer almost killed my terminal, as it still `cat`ed the pointed file content. – Ömer An Aug 31 '16 at 08:35
  • @ÖmerAn Not certain if requirement is possible with `javascript` in browser alone, though `nodejs` and `browserify` could probably be used. Tried `cat` with a `.desktop` file, for example, see http://stackoverflow.com/questions/37557098/html-drag-drop-how-to-set-the-filename-of-an-outgoing-drag-to-desktop/37777050#37777050 ; try dragging the file at http://plnkr.co/edit/pif0ZraAn9RbJI8w2z0w?p=preview to GUI at file manager, then run `$ cat filename`. Though example links to an external file `URL`. You can also use `$ file -h filename`. – guest271314 Aug 31 '16 at 15:31