Google automatic redirect

I know this question sounds too generic but I am unable to find an answer for it. How can I create a link (soft/hard) file which when opened, redirect me to a website with http protocol? One possible way I can think of is creating a lame shell script and making it executable which can make use of a browser to open a website. But isn’t there a concept of a «hyperlink file»?

6 Answers 6

Now if you run firefox target.html it will open example.com.

To clarify, there is no such thing as a «hyperlink file». You may have heard of symbolic and hard links, but those are simply a way to refer to a file on disk — they are not URLs. From man ln :

Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

$ cat ~/Desktop/Link.desktop [Desktop Entry] Encoding=UTF-8 Name=Link to your site URL=http://your-site-url.com Icon=text-html 

This is a possible answer when your solution needn’t be command-line-based, but supposed to gain much comfort.

Of course the .desktop files don’t have to be stored in ~/Desktop/ . I just used this location since they might be used in that location mostly.

This is missing Type=Link ; the key is unconditionally required, and the value is required in order to use it as a web link file.

xdbg-open is default application for open anything, so you can create bash script like this:

#!/bin/bash if which xdg-open > /dev/null then xdg-open YOUR_URL elif which gnome-open > /dev/null then gnome-open YOUR_URL fi 

Replace YOUR_URL accordingly, save file and make it executable ( chmod +x filename.sh )

One answer that has not come up yet, is using Linux’s binfmt_misc capabilities, to create a native executable link format that works on the kernel level!

To automatically open any .link files in xdg-open , put this inside an executable file at /etc/local.d/binfmt_misc.start , or any file your OS runs on startup:

#! /bin/sh echo ':open-hyperlink:E::link::/usr/local/bin/open-hyperlink:' > /proc/sys/fs/binfmt_misc/register 

and put the following into the /usr/local/bin/open-hyperlink executable:

After that, you can just “run” .link files that are marked as executable via any means, and it will open the link in the browser. Command line, GUI double click, whatever you like.

$ echo 'http://superuser.com/questions/986527/how-to-create-a-hyperlink-file' > this-page.link $ chmod +x this-page.link $ ./this-page.link [Browser opens…] 

You can of course change the extension and file format how you like, provided you change the open-hyperlink script accordingly. Even Windows .lnk files!

Of course your kernel has to have that module available and enabled, for it to work. (I have it compiled in.)

Check out the documentation on binfmt_misc , as there are a lot more possibilities, e.g. with matching on a pattern instead of a file extension.

Источник

Shell script to open a URL

How do I write a simple shell script (say script.sh), so that I can pass a URL as an argument while executing? I want a browser to start with the page opened on that URL. I want to write the command in the script to open a browser and open the URL given in argument.

Читайте также:  Chmod linux исполняемый файл

if you want a python solution, have a look at webbrowser.open . In a cli environment, you can just do python -m webbrowser https://google.com , provided you have python installed (most linux distributions do)

8 Answers 8

You don’t need to write a script for that. There’re some tools that you can use depending on your OS:

Linux

xdg-open is available in most Linux distributions. It opens a file or URL in the user’s preferred browser (configurable with xdg-settings ).

xdg-open https://stackoverflow.com 

macOS

open opens files and URLs in the default or specified application.

open https://stackoverflow.com open -a Firefox https://stackoverflow.com 

Windows

You can use the start command at the command prompt to open an URL in the default (or specified) browser.

start https://stackoverflow.com start firefox https://stackoverflow.com 

Cross-platform

The builtin webbrowser Python module works on many platforms.

python3 -m webbrowser https://stackoverflow.com

Any way to tell it which browser to try first, eg Chrome. On Mac it tries firefox then falls back to safari

Thanks for this solution, now i can put this in auto start script and make sure that every time I start my PC It will open stackoverflow first and hopefully in 100 day I will get a golden badge 🙂

Suppose your browser is Firefox and your script urlopener is

Replace firefox with your browser’s executable file name.

As [ @sato-katsura ] mentioned in the comment, in *nixes you can use an application called xdg-open . For example,

The manual for xdg-open says

xdg-open — opens a file or URL in the user’s preferred application xdg-open opens a file or URL in the user’s preferred application. If a URL is provided the URL will be opened in the user’s preferred web browser.
If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs.

As [ this ] answer points out you could change your preferred browser using say:

xdg-settings set default-web-browser firefox.desktop 
xdg-settings set default-web-browser chromium-browser.desktop 

Источник

Is there any way to create a symbolic link to a (HTTP) URL? Update: The reason I want to do this is so that I can move this symlink to another computer without having to copy the file itself (it’s big), and instead, the other computer will just use the online copy from the URL.

URL’s have little to do with HTTP (assuming that’s what’s implied). That’s why it’s explicitly http:// at the beginning. You need to be more specific. Wouldn’t just mounting WebDAV do, using davfs , again assuming you’re on Linux?

Consider using WebDAV. Otherwise, most systems allow the storing of URL references, e.g. .url files on Windows or .webloc files on Mac OS X.

Is there a way to have a file on my hard drive automatically kept up-to-date with a version that’s on the web? (But not deleted or overwritten with garbage if the online version disappears or changes, of course.) Should that be a different question?

Читайте также:  Install 2 linux on one computer

@NotMe If you just want to create a cross-platform internet shortcut (using an HTML page that automatically redirects to another page), then see here: superuser.com/questions/538089/…

8 Answers 8

It’s not possible to create a symlink to an URL. If you can make executables and the target OS is Linux-alike, you can create a file which opens the URL as in:

#!/bin/sh x-www-browser 'http://example.com/your/link' 

If your default browser is Firefox and your also have Chrome installed, Chrome might open the URL due to a Firefox bug with «alternatives» system in dpkg. bugzilla.mozilla.org/show_bug.cgi?id=1218174

For ease of use, you can take this script to generate links like suggested: superuser.com/a/1524254/910769

It would be more proper to use xdg-open rather than x-www-browser to be compatible with most distributions.

Hey, this answer is incorrect. You could potentially use an S3 bucket (which is accessible by a URL) and then mount the bucket to your file system using FUSE — check out github.com/kahing/goofys — I haven’t tested it but I’m pretty sure this is one way of doing it. I’m still looking for the solution that allows me to symlink arbitrary URL/files

If you are using a GUI desktop in Linux, like Gnome or Unity, you can drag and drop a URL from Firefox and other browsers either onto the desktop or into a folder in the Nautilus file manager. This will create a .desktop file with several lines in it like this one:

[Desktop Entry] Encoding=UTF-8 Name=Link to Google Calendar Type=Link URL=https://www.google.com/calendar/render?pli=1 Icon=text-html 

As long as you are in the GUI, you can just double-click on the file to open it into the default Web browser. I do this in Ubuntu to store links to documents in my private Drupal wiki on my machine.

This may work for KDE, xfce, and other desktop managers, but I haven’t tried it.

You can consider the script provided here to automatically generate such a .desktop file. They can also be used for other stuff as e.g. executing a command as described in my answer there.

You want an automatic URL link, stored in a file in your file system, to open.

The way to do this is with a minimalist .HTML file. For example, to take you to the Google home page, place the following code in a file named Google.HTML :

     

For older browsers, click Redirect

Redirect

When you open (i.e double click) on this file, the OS will open your default browser (e.g. Firefox) and render this little HTML file, which has a URL redirect in the header, which in turn will automatically open the URL in the redirect.

This can be adapted to take you to the online file as per your question.

The URL contains the protocol (e.g. HTTP), so just make sure that it’s in there. To be more minimalist, you can omit the and lines.

I tried the other answers on this page with Ubuntu 16.04 without success, but this solution works.

Читайте также:  Rabbitmq установка и настройка linux

This is a better solution as it can be served, unlike a shell script. Most browsers will let you get away with just provided you use the extension .html

It’s not possible to link to a HTTP location. You might be able to mount the location of this file via WebDAV to your system and link to the local mount, but this only works if it’s configured to get exported via WebDAV..
But if you want to read the file (I think you are trying to do so), you anyhow have to download the content (even if it would be possible to create such a link). So I recommend to simply download it.

-1 as it is possible as seen in other answers and the op stated that the reason he wants to do this is so that he could move this symlink to another computer without having to copy the file itself (it’s big), and instead, the other computer will just use the online copy from the URL.

@Cadoiz The solutions provided in other answers are not links. They are either shell scripts (running a web browser), HTML files that perform redirection (opened via web browser), or .desktop files (a GUI/file manager feature that opens a web browser). A link, in strict sense, to a HTTP URL cannot be created, as links are a filesystem feature and work only within filesystems. The OP clearly stated he wants a symlink. This is not possible. If he’d say «shortcut» (which is not strictly defined), then yes, all the other answers can be applied.

For ease of use, I wrote a script to generate bash-links like Lekensteyn suggested in the accepted answer. This is also able to properly handle arguments containing spaces or other special characters.

Making it runnable makes things even more handy. Run it like $ linkscript.sh «http://example.com/your/link» «YourLinkFile.sh» . (Depending on your link and file name, this usually also works without the quotes » . As a rule of thumb, it is always safer with quotes.)

#!/bin/bash printf '#!/bin/bash\nxdg-open %q\n' "$1" > "$2" && chmod +x -- "$2" #Overwrite existing file with > instead of appending with >> chmod +x $2 #Makes the generated script executeable 

Thanks also for Lekensteyn’s former comment suggesting the use of printf %q format specifier to ensure that special characters are properly quoted in case the URLs containing single quotes. This also simplified/shortened the code a little. The suggested script then uses the single printf line instead of the previous two-liner, which can be seen in the edit history. Thanks also to altermetax’ comment for pointing to use xdg-open rather than x-www-browser to be compatible with most distributions.

Note about the shebang in the code:

You have to use #!/bin/bash instead of #!/bin/sh to get the correct version of printf and don’t cause printf: %q: invalid directive as per this Q/A. Thanks to M.T for pointing out this improvement!

Источник

Оцените статью
Adblock
detector